PySide:将QComboBox索引与QWidgets连接

时间:2014-10-26 09:43:49

标签: python qt pyqt pyside

我想连接我的QComboBox索引并打开新的小部件。我想这样做,当选择另一个QComboBox索引时,先前打开的小部件关闭。现在小部件只是一个接一个地加起来。

from PySide import QtGui, QtCore
import pandas as pd
import pyqtgraph as pg


class Widget(QtGui.QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)

        v_global_layout = QtGui.QVBoxLayout()

        method_selection = QtGui.QComboBox()
        method_selection.addItem("A")
        method_selection.addItem("B")

        v_global_layout.addWidget(method_selection)
        v_global_layout.setAlignment(QtCore.Qt.AlignTop)

        def change_widget(index):
            a = Graph()
            b = TabDialog()

            if index == 0:
                v_global_layout.addWidget(a)
            elif index == 1:
                v_global_layout.addWidget(b)

        method_selection.activated.connect(change_widget)

        self.setLayout(v_global_layout)

class TabDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        tab_widget = QtGui.QTabWidget()
        tab_widget.addTab(QtGui.QWidget(), "1")
        tab_widget.addTab(QtGui.QWidget(), "2")

        main_layout = QtGui.QVBoxLayout()
        main_layout.addWidget(tab_widget)
        self.setLayout(main_layout)


class Graph(QtGui.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.fig = pg.PlotWidget(name='PlotWidget')
        self.plot_area = self.fig.plotItem

        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.fig)
        self.setLayout(layout)


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)

    main_window = Widget()
    main_window.setGeometry(100, 100, 640, 480)
    main_window.show()

    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:1)

我的问题的解决方案是使用QStackedWidget。

from PySide import QtGui, QtCore
import pandas as pd
import pyqtgraph as pg


class Widget(QtGui.QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)

        v_global_layout = QtGui.QVBoxLayout()

        method_selection = QtGui.QComboBox()
        method_selection.addItem("A")
        method_selection.addItem("B")

        self.stackedWidget = QtGui.QStackedWidget()
        self.stackedWidget.addWidget(TabDialog())
        self.stackedWidget.addWidget(Graph())

        method_selection.activated[int].connect(self.stackedWidget.setCurrentIndex)

        v_global_layout.addWidget(method_selection)
        v_global_layout.setAlignment(QtCore.Qt.AlignTop)
        v_global_layout.addWidget(self.stackedWidget)
        self.setLayout(v_global_layout)

class TabDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        tab_widget = QtGui.QTabWidget()
        tab_widget.addTab(QtGui.QWidget(), "1")
        tab_widget.addTab(QtGui.QWidget(), "2")

        main_layout = QtGui.QVBoxLayout()
        main_layout.addWidget(tab_widget)
        self.setLayout(main_layout)


class Graph(QtGui.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.fig = pg.PlotWidget(name='PlotWidget')
        self.plot_area = self.fig.plotItem

        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.fig)
        self.setLayout(layout)


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)

    main_window = Widget()
    main_window.setGeometry(100, 100, 640, 480)
    main_window.show()

    sys.exit(app.exec_())

答案 1 :(得分:1)

我可以建议你更好的方式。

有特殊的小部件QStackedWidget。您可以添加不同的小部件,并使用QComboBox中的activated信号。您还可以使用setCurrentIndex更改小部件。