python pyqt and parent class

為{幸葍}努か 提交于 2019-12-23 05:42:04

问题


class testWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(testWidget, self).__init__(parent)
        self.parent = parent
        self.something()
    def something(self):
        self.parent.callme() # self.parent?.... nice?


class testClass():
    def __init__(self):
        self.widget = testWidget(parent=self)

test = testClass()

What is the cleanest way of dealing with a parent class in python(pyqt)? Is there a nicer way than calling self.parent directly?


回答1:


If you want to call a method of this widget's parent (if one has been set), use QObject.parent():

class TestWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(TestWidget, self).__init__(parent)
    def something(self):
        self.parent().callMe()

class TestClass(QtCore.QObject):
    def __init__(self):
        super(TestClass, self).__init__(None)
        self.widget = TestWidget(parent=self)
        ...
    def callMe(self): pass

test = TestClass()


来源:https://stackoverflow.com/questions/11863114/python-pyqt-and-parent-class

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