0

我有 QTabWidget,以及我想在这个 QTabWidget 选项卡中显示的一些小部件和子布局的布局。我想要做的是将此布局添加到第一个选项卡(默认情况下),如果用户移动到下一个选项卡,我想显示完全相同的布局,并在它附近添加一些小部件。这是我正在谈论的布局:

    self.right_tab_layout = QVBoxLayout()
    self.right_tab_widget = QWidget()
    self.right_tab_title_label = QLabel("Select full files path:")
    self.simoderev_layout = QHBoxLayout()
    self.simoderev_widget = QWidget()
    self.simoderev_checkbox = QCheckBox("use simoderev as base: ")
    self.simoderev_combobox = QComboBox()
    self.paths_label = QLabel("paths:")

    self.right_tab_widget.setLayout(self.right_tab_layout)

    self.simoderev_widget.setLayout(self.simoderev_layout)
    self.simoderev_widget.setMaximumWidth(250)

    self.simoderev_layout.addWidget(self.simoderev_checkbox)
    self.simoderev_layout.addWidget(self.simoderev_combobox)
    self.simoderev_layout.setContentsMargins(0,0,0,0)

    self.right_tab_layout.addWidget(self.right_tab_title_label)
    self.right_tab_layout.addWidget(self.simoderev_widget)
    self.right_tab_layout.addWidget(self.paths_label)

有什么办法吗?

4

1 回答 1

1

如果您只是希望选项卡中的小部件看起来相同,则应该创建一个自定义 Widget 类并将该类的实例放在每个选项卡中。

您的自定义小部件可能如下所示:

class CustomWidget(QtGui.QWidget):

    def __init__(self, parent=None):
        super(CustomWidget, self).__init__(parent)
        layout = QtGui.QVBoxLayout(self)
        self.title_label = QtGui.QLabel("Select full files path:")
        self.simoderev_widget = QtGui.QWidget()
        simoderev_layout = QtGui.QHBoxLayout(self.simoderev_widget)
        self.simoderev_checkbox = QtGui.QCheckBox("use simoderev as base: ")
        self.simoderev_combobox = QtGui.QComboBox()
        self.simoderev_widget.setMaximumWidth(250)
        simoderev_layout.addWidget(self.simoderev_checkbox)
        simoderev_layout.addWidget(self.simoderev_combobox)
        simoderev_layout.setContentsMargins(0,0,0,0)
        self.paths_label = QtGui.QLabel("paths:")
        layout.addWidget(self.title_label)
        layout.addWidget(self.simoderev_widget)
        layout.addWidget(self.paths_label)

如果您希望它们相同,这是一个 hacky 解决方案。您应该将currentChangedtabwidget 的信号连接到一个插槽,该插槽会将您的自定义小部件从一个选项卡移动到另一个选项卡。

class MyTabWidget(QtGui.QTabWidget):

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

        self.subwidget = CustomWidget(self)

        self.left_tab_widget = QtGui.QWidget()
        self.leftLayout = QtGui.QVBoxLayout(self.left_tab_widget)
        self.leftLayout.addWidget(self.subwidget)

        self.right_tab_widget = QtGui.QWidget(self)
        self.rightLayout = QtGui.QVBoxLayout(self.right_tab_widget)
        label = QtGui.QLabel("Some additional data", self.right_tab_widget)
        self.rightLayout.addWidget(label)

        self.addTab(self.left_tab_widget, "Left Tab")
        self.addTab(self.right_tab_widget, "Right Tab")

        self.currentChanged.connect(self.onCurrentChanged)

    def onCurrentChanged(self, index):
        if index == 0:
            self.leftLayout.addWidget(self.subwidget)
        else:
            self.rightLayout.addWidget(self.subwidget)


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    widget = MyTabWidget()
    widget.show()
    app.exec_()
于 2013-10-15T12:39:38.610 回答