PySide - 从中​​央窗口小部件中重置主窗口中央窗口小部件

时间:2014-10-07 14:26:39

标签: python qt python-3.x pyside

我正在尝试创建一个GUI窗口,该窗口打开时带有一个中央窗口小部件,显示了几个可供选择的选项。每个选项都根据用户选择用新窗口替换中央窗口小部件。类似于PyCharm和QtDesigner等程序的开放。

目前我无法完成这项工作。这就是我尝试过的:

from PySide import QtCore, QtGui
import sys

class ControlMainWidget(QtGui.QWidget):
    def __init__(self, parent=None, Widg = Opener()):
        super(ControlMainWidget, self).__init__(parent)
        self.widget = Widg
        self.widget.setupUi(self)
        if isinstance(Widg, Opener):
            print(1)
            QtCore.QObject.connect(self.widget.toolButton_3, QtCore.SIGNAL('clicked()'), MainWidgetChange(parent, widg=StatementView()))
        print(2)


class ControlMainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(ControlMainWindow, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

def MainWidgetChange(Window, widg = Opener()):
    the_widget = ControlMainWidget(parent = Window, Widg = widg)

    Window.setCentralWidget(the_widget)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    mySW = ControlMainWindow()
    MainWidgetChange(mySW)
    mySW.show()
    sys.exit(app.exec_())

Opener和StatementView是使用qtDesigner创建的类,所以有点罗嗦。我将包括Opener,但是StatementView大约是250行,所以我将其留在这里。

class Opener(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(450, 400)
        Form.setMinimumSize(QtCore.QSize(450, 400))
        Form.setMaximumSize(QtCore.QSize(450, 400))
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setObjectName("gridLayout")
        self.toolButton = QtGui.QToolButton(Form)
        self.toolButton.setMinimumSize(QtCore.QSize(150, 150))
        self.toolButton.setObjectName("toolButton")
        self.gridLayout.addWidget(self.toolButton, 0, 0, 1, 1)
        self.toolButton_2 = QtGui.QToolButton(Form)
        self.toolButton_2.setMinimumSize(QtCore.QSize(150, 150))
        self.toolButton_2.setObjectName("toolButton_2")
        self.gridLayout.addWidget(self.toolButton_2, 0, 1, 1, 1)
        self.toolButton_3 = QtGui.QToolButton(Form)
        self.toolButton_3.setMinimumSize(QtCore.QSize(150, 150))
        self.toolButton_3.setObjectName("toolButton_3")
        self.gridLayout.addWidget(self.toolButton_3, 1, 0, 1, 1)
        self.toolButton_4 = QtGui.QToolButton(Form)
        self.toolButton_4.setMinimumSize(QtCore.QSize(150, 150))
        self.toolButton_4.setObjectName("toolButton_4")
        self.gridLayout.addWidget(self.toolButton_4, 1, 1, 1, 1)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)


    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.toolButton.setText(QtGui.QApplication.translate("Form", "Tables", None, QtGui.QApplication.UnicodeUTF8))
        self.toolButton_2.setText(QtGui.QApplication.translate("Form", "Graphs", None, QtGui.QApplication.UnicodeUTF8))
        self.toolButton_3.setText(QtGui.QApplication.translate("Form", "Statements", None, QtGui.QApplication.UnicodeUTF8))
        self.toolButton_4.setText(QtGui.QApplication.translate("Form", "Modelling", None, QtGui.QApplication.UnicodeUTF8))

当窗口加载Opener作为中心窗口小部件时 - 我也可以硬编码将StatementView设置为中央窗口小部件而没有错误。

然而,当我使用上述功能时,在开启器中单击工具按钮时,没有任何反应。我试图替换这一行:

QtCore.QObject.connect(self.widget.toolButton_3, QtCore.SIGNAL('clicked()'), MainWidgetChange(parent, widg=StatementView()))

QtCore.QObject.connect(self.widget.toolButton_3, QtCore.SIGNAL('clicked()'), MainWidgetChange(self.parent, widg=StatementView()))

会引发以下错误:

Traceback (most recent call last):
  File "/home/milo/Documents/Codes/Statements/New test code/mainui2.py", line 593, in <module>
    MainWidgetChange(mySW)
  File "/home/milo/Documents/Codes/Statements/New test code/mainui2.py", line 586, in MainWidgetChange
    the_widget = ControlMainWidget(parent = Window, Widg = widg)
  File "/home/milo/Documents/Codes/Statements/New test code/mainui2.py", line 575, in __init__
    QtCore.QObject.connect(self.widget.toolButton_3, QtCore.SIGNAL('clicked()'), MainWidgetChange(self.parent, widg=StatementView()))
  File "/home/milo/Documents/Codes/Statements/New test code/mainui2.py", line 586, in MainWidgetChange
    the_widget = ControlMainWidget(parent = Window, Widg = widg)
  File "/home/milo/Documents/Codes/Statements/New test code/mainui2.py", line 570, in __init__
    super(ControlMainWidget, self).__init__(parent)
TypeError: 'PySide.QtGui.QWidget' called with wrong argument types:
  PySide.QtGui.QWidget(builtin_function_or_method)
Supported signatures:
  PySide.QtGui.QWidget(PySide.QtGui.QWidget = None, PySide.QtCore.Qt.WindowFlags = 0)

并且无法加载主窗口。

有没有一种标准的方法可以解决这个问题?我不确定此刻还有什么可以尝试。

1 个答案:

答案 0 :(得分:0)

固定!

我忘了连接事件应该使用函数作为对象(而不是括号/参数) - 我将函数包含在lambda中的connect事件中。现在一切都有效。

所以:

QtCore.QObject.connect(self.widget.toolButton_3, QtCore.SIGNAL('clicked()'), MainWidgetChange(parent, widg=StatementView()))

要:

QtCore.QObject.connect(self.widget.toolButton_3, QtCore.SIGNAL('clicked()'), lambda: MainWidgetChange(parent, widg=StatementView()))

相关问题