About the super() function

北战南征 提交于 2019-12-24 19:22:35

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!