关于super()函数 [英] About the super() function

查看:91
本文介绍了关于super()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用QtDesign创建自己的UI并将其转换为python版本.因此,在将UI python文件子类化之后,我编写了一些函数来实现QGraphicsView的mouseEvent.只是一个小问题.如何为QGraphicsView调用super()函数?

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)

类似于super(RigModuleUi,self).mousePressEvent(event),它将为QMainWindow(而不是QGraphicsView)返回mouseEvent.因此,像rubberBand这样的鼠标其他所有选项都将丢失.

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.

谢谢

推荐答案

我不太确定您希望在这里发生什么.您正在存储绑定方法.调用它时,仍会使用与存储时相同的self来调用它.

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.

您的super正走在RigModuleUi的祖先上,该祖先不继承QGraphicsView.

Your super is walking up the ancestry of RigModuleUi, which doesn't inherit from QGraphicsView.

self.GraphicsView是实例属性的有趣名称;那应该是一个班级的名称,还是只是大写? (请遵循 PEP8命名约定.)如果您将该方法定义为全局函数,并为该实例分配了那个.

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

在这里疯狂地猜测;我不知道PyQt的类层次结构:)

Guessing wildly here; I don't know PyQt's class hierarchy :)

这篇关于关于super()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆