获取组合框来设置显示的小部件?

Getting a comboBox to set the shown widget?

我正在为我编写的 python 程序制作 GUI,并希望根据 comboBox 的选择,让一个区域显示不同的 widget

这是一个MWE:

我希望此 Main Window 中的轮廓区域显示 TextWidgetCalendarWidget,具体取决于 comboBox 是否设置为 Option 1Option 2

所有 GUI 都是使用 Qt Designer 构建的,python 文件使用 uic 加载它们。

import sys
from PyQt4 import QtCore, QtGui, uic

Main = "Main.ui"
MainUI, MainBase = uic.loadUiType(Main)
Text = "textwidget.ui"
TWidget, TBase = uic.loadUiType(Text)
Cal = "calendarwidget.ui"
CWidget, CBase = uic.loadUiType(Cal)

class OperatorGUI(MainBase, MainUI):
    def __init__(self, parent=None):
        super(OperatorGUI, self).__init__(parent)
        self.setupUi(self)
        self.comboBox.activated[str].connect(self.choose_widget)


    def choose_widget(self, choice):
        # Set the widget accorging to the choice
        #if choice == "Option 1":
        print choice


class TextWidget(TBase, TWidget):
    def __init__(self):
        # Something
        print "Text"

class CalendarWidget(CBase, CWidget):
    def __init__(self):
        # Something
        print "Calendar"


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = OperatorGUI()
    window.show()
    sys.exit(app.exec_())

如果我使用 QtDockWidget 我不能把它放在我想要的地方。我不希望它是可拆卸的或弹出一个单独的 window.

我该如何解决这个问题?

编辑

在@eyllanesc 的帮助下,代码现在看起来像这样:

import sys
from PyQt4 import QtCore, QtGui, uic

Main = "Main.ui"
MainUI, MainBase = uic.loadUiType(Main)
Text = "textwidget.ui"
TWidget, TBase = uic.loadUiType(Text)
Cal = "calendarwidget.ui"
CWidget, CBase = uic.loadUiType(Cal)

class OperatorGUI(MainBase, MainUI):
    def __init__(self, parent=None):
        super(OperatorGUI, self).__init__(parent)
        self.setupUi(self)
        self.comboBox.activated[str].connect(self.choose_widget)

        self.ChangingWidget.setLayout(QtGui.QVBoxLayout())
        self.stacked = QtGui.QStackedWidget()
        self.ChangingWidget.layout().addWidget(self.stacked)
        self.textWidget = TextWidget()
        self.calendarWidget = CalendarWidget()
        self.stacked.addWidget(self.textWidget)
        self.stacked.addWidget(self.calendarWidget)


    def choose_widget(self, choice):
        # Set the widget accorging to the choice
        if choice == "Option 1":
            self.stacked.setCurrentWidget(self.textWidget)
        elif choice == "Option 2":
            self.stacked.setCurrentWidget(self.calendarWidget)
        print choice
        self.setupUi(self)
        self.show()


class TextWidget(TBase, TWidget):
    def __init__(self, parent=None):
        super(TextWidget, self).__init__(parent)
        self.setParent(parent)
        print "Text"

class CalendarWidget(CBase, CWidget):
    def __init__(self, parent=None):
        super(CalendarWidget, self).__init__(parent)
        self.setParent(parent)
        print "Calendar"


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = OperatorGUI()
    window.show()
    sys.exit(app.exec_())

要完成此任务,建议使用 QStackedWidget,为此我假设可以通过 self.widget:

访问与该区域关联的小部件
class OperatorGUI(MainBase, MainUI):
    def __init__(self, parent=None):
        super(OperatorGUI, self).__init__(parent)
        self.setupUi(self)
        self.comboBox.activated[str].connect(self.choose_widget)

        self.widget.setLayout(QVBoxLayout())
        self.stacked = QtGui.QStackedWidget()
        self.widget.layout().addWidget(self.stacked)
        self.textWidget = TextWidget()
        self.calendarWidget = CalendarWidget()
        self.stacked.addWidget(self.textWidget)
        self.stacked.addWidget(self.calendarWidget)

    def choose_widget(self, choice):
        # Set the widget accorging to the choice
        if choice == "Option 1":
            self.stacked.setCurrentWidget(self.textWidget)
        elif choice == "Option 2":
            self.stacked.setCurrentWidget(self.calendarWidget)