哪个是 child class ChildWidget (PySide) 的父级?

Which one is a parent of child class ChildWidget (PySide)?

你能帮我解释一下为什么 print(str(self.parent())) returns MainWindow 和 self.print_base() returns QWidget 吗? parent() 方法在哪里定义?在 super(ChildWidget, self).__init__(parent) 父级转到 MainWindow init 或在 QWidget init?

import sys
from PySide import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.do_something() #sanity check
        self.cw = ChildWidget(self)
        self.setCentralWidget(self.cw)
        self.show()

    def do_something(self):

        print 'doing something!'

class ChildWidget(QtGui.QWidget):
    def print_base(self):
        for base in self.__class__.__bases__:
            print base.__name__

    def __init__(self, parent):
        super(ChildWidget, self).__init__(parent)

        print(str(self.parent()))
        self.print_base()

        self.button1 = QtGui.QPushButton()
        self.button1.clicked.connect(self.do_something_else)

        self.button2 = QtGui.QPushButton()
        self.button2.clicked.connect(self.parent().do_something)

        self.layout = QtGui.QVBoxLayout()
        self.layout.addWidget(self.button1)
        self.layout.addWidget(self.button2)
        self.setLayout(self.layout)
        self.show()

    def do_something_else(self):
        print 'doing something else!'

您正在处理两种类型的层次结构:1) 小部件层次结构; 2) python 类 层次结构。 "print_base" 方法在 python POV 中列出所有基础 类,而 "parent" returns 子控件所附加的控件实例。