我有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)
有没有办法做到这一点?
答案 0 :(得分: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解决方案。您应该将tabwidget的currentChanged
信号连接到一个插槽,以便将自定义小部件从一个选项卡移动到另一个选项卡。
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_()