我正在嘗試創建一個GUI窗口,其中打開一箇中央窗口小部件,顯示可供選擇的幾個選項。每個選項都會根據用戶的選擇用新窗口替換中央小部件。與PyCharm和QtDesigner等程序的開放類似。PySide - 重置中央窗口小部件中的主窗口中央窗口小部件
目前我無法完成這項工作。這是我試過的:
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製作的類,所以有點羅嗦。我將包括開瓶器,但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))
此刻的窗口加載與開瓶器爲中心的小部件 - 我還可以硬編碼它來設置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)
,並無法加載主窗口。
那麼有沒有一種標準的做法,我錯過了?我不確定此刻還有什麼可以嘗試的。