问题描述PyQt5中,想在QGraphicsScene的mousePressEvent中获取鼠标位置,无论点哪,显示的都是[0.0,0.0]问题出现的环境背景及自己尝试过哪些方法网上没找到类似的问题相关代码//请把代码文本粘贴到下方(请勿用图片代替代码)#这是继承QGraphicsScene的自定义类classCARscene(QtWidgets.QGraphicsScene):def__init__(self,parent=None):super(CARscene,self).__init__(parent)defmousePressEvent(self,QMouseEvent):#这行代码是期望显示坐标,奈何永远都是[0.0,0.0]print([QMouseEvent.pos().x(),QMouseEvent.pos().y()])#这是主窗口的类,继承自QtDesigner设计的界面classCDataMingQtUi(QtWidgets.QMainWindow,Ui_MainWindow):def__init__(self):super(CDataMingQtUi,self).__init__()self.setupUi(self)#CARgraphview是一个QGraphicView的实例self.figure=plt.figure()self.canvas=FigureCanvas(self.figure)self.graph_sence=CARscene()self.graph_sence.addWidget(self.canvas)self.CARgraphview.setScene(self.graph_sence)你期待的结果是什么?实际看到的错误信息又是什么?期待显示鼠标单击的坐标实际显示的确是[0.0,0.0],无论点哪都是这个值
2 回答
LEATH
TA贡献1936条经验 获得超6个赞
改成用scenePosdefmousePressEvent(self,event):QGraphicsScene.mousePressEvent(self,event)e=event.scenePos()print(e)
holdtom
TA贡献1805条经验 获得超10个赞
一,引入QCursorfromPyQt5.QtimportQCursor二.绑定本类的eventFilterself.pb_heading.installEventFilter(self)三,在qevent.pos()处弹窗,即鼠标所在位置defeventFilter(self,qobject,qevent):qtype=qevent.type()#~print(qtype)#~ifqtype==QEvent.HoverMove:ifqtype==QEvent.HoverLeave:print("HoverLeave")returnTrueelifqtype==QEvent.HoverEnter:print("HoverEnter")print(qevent.pos())self.on_pb_heading_clicked(qevent.pos())returnTrueelse:returnFalse
添加回答
举报
0/150
提交
取消