问题
I'm using QtDesign to create my own UI and convert it to python version. So after subclass the UI python file, i had written some function to implement mouseEvent for QGraphicsView. Just one small question. How can i call the super() function for the QGraphicsView?
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)
Look like the super(RigModuleUi,self).mousePressEvent(event)
will return the mouseEvent for QMainWindow, not QGraphicsView. So all other option for mouse like rubberBand will lost.
Thanks
回答1:
I'm not quite sure what you expect to happen here. You're storing a bound method. When it's called, it'll still be called with the same self
it had when you stored it.
Your super
is walking up the ancestry of RigModuleUi
, which doesn't inherit from QGraphicsView
.
self.GraphicsView
is a funny name for an instance attribute; is that supposed to be the name of a class, or is it just capitalized incidentally? (Please follow PEP8 naming conventions.) Perhaps you'd have more luck if you defined the method as a global function and assigned that to the instance.
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
Guessing wildly here; I don't know PyQt's class hierarchy :)
来源:https://stackoverflow.com/questions/15131282/about-the-super-function