关于super()函数
我正在使用QtDesign来创建自己的用户界面,并把它转换成Python版本。在我对UI的Python文件进行子类化之后,我写了一些函数来处理QGraphicsView的鼠标事件。现在我有一个小问题。请问我该如何调用QGraphicsView的super()函数呢?
class RigModuleUi(QtGui.QMainWindow,Ui_RiggingModuleUI):
def __init__(self,parent = None):
super(RigModuleUi,self).__init__(parent = parent)
self.GraphicsView.mousePressEvent = self.qView_mousePressEvent
def qView_mousePressEvent(self,event):
if event.button() == QtCore.Qt.LeftButton:
super(RigModuleUi,self).mousePressEvent(event)
看起来super(RigModuleUi,self).mousePressEvent(event)
会返回QMainWindow的鼠标事件,而不是QGraphicsView的。所以像橡皮带这样的鼠标操作就会失效。
谢谢!
1 个回答
0
我不太确定你希望这里发生什么。你存储的是一个绑定的方法。当它被调用时,仍然会使用你存储时的那个self
。
你的super
是在查找RigModuleUi
的父类,而这个类并没有继承自QGraphicsView
。
self.GraphicsView
这个名字听起来有点奇怪,作为实例属性,它是应该是一个类的名字,还是只是偶然大写了?(请遵循PEP8命名规范。)也许如果你把这个方法定义为一个全局函数,然后把那个赋值给实例,效果会更好。
def qView_mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
super(QGraphicsView, self).mousePressEvent(event)
class RigModuleUi(QtGui.QMainWindow, Ui_RiggingModuleUI):
def __init__(self, parent=None):
super(RigModuleUi,self).__init__(parent=parent)
self.GraphicsView.mousePressEvent = qView_mousePressEvent
我在这里胡乱猜测;我对PyQt的类层次结构不太了解 :)