Search code examples
pythonpyqtqt-creatorpyqt5qsplitter

Implementing splitter with PyQt (QtCreator generated file)


I use QtCreator but I cannot find QSplitter in the UI builder so I guess it's added later with the rest of the code. My question is:

How to add a QSplitter without screwing my GUI?

This is the basis layout look of my main window: Layout

I need to have a splitter between the tabWidget and the QlistWidget on the left. I need only those two elements to be affected. I found out that they need to be on a separate layout (they are) but how to add a Qsplitter and not affect the others. Also when I add them to splitter, should it be before adding them to parent layout of after. To make it more clear this is the generated code from the screenshot:

def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(1135, 640)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
    self.verticalLayout.setObjectName("verticalLayout")
    self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_3.setObjectName("horizontalLayout_3")
    self.verticalLayout_3 = QtWidgets.QVBoxLayout()
    self.verticalLayout_3.setObjectName("verticalLayout_3")
    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setObjectName("label")
    self.verticalLayout_3.addWidget(self.label)
    self.horizontalSlider_2 = QtWidgets.QSlider(self.centralwidget)
    self.horizontalSlider_2.setOrientation(QtCore.Qt.Horizontal)
    self.horizontalSlider_2.setObjectName("horizontalSlider_2")
    self.verticalLayout_3.addWidget(self.horizontalSlider_2)
    self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_4.setObjectName("horizontalLayout_4")
    self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_4.setObjectName("pushButton_4")
    self.horizontalLayout_4.addWidget(self.pushButton_4)
    self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_6.setObjectName("pushButton_6")
    self.horizontalLayout_4.addWidget(self.pushButton_6)
    self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_5.setObjectName("pushButton_5")
    self.horizontalLayout_4.addWidget(self.pushButton_5)
    self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_7.setObjectName("pushButton_7")
    self.horizontalLayout_4.addWidget(self.pushButton_7)
    self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_8.setObjectName("pushButton_8")
    self.horizontalLayout_4.addWidget(self.pushButton_8)
    self.verticalLayout_3.addLayout(self.horizontalLayout_4)
    self.horizontalLayout_3.addLayout(self.verticalLayout_3)
    self.verticalLayout_4 = QtWidgets.QVBoxLayout()
    self.verticalLayout_4.setObjectName("verticalLayout_4")
    self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_5.setObjectName("horizontalLayout_5")
    self.label_3 = QtWidgets.QLabel(self.centralwidget)
    self.label_3.setObjectName("label_3")
    self.horizontalLayout_5.addWidget(self.label_3)
    self.label_2 = QtWidgets.QLabel(self.centralwidget)
    self.label_2.setObjectName("label_2")
    self.horizontalLayout_5.addWidget(self.label_2)
    self.verticalLayout_4.addLayout(self.horizontalLayout_5)
    self.horizontalSlider_3 = QtWidgets.QSlider(self.centralwidget)
    self.horizontalSlider_3.setOrientation(QtCore.Qt.Horizontal)
    self.horizontalSlider_3.setObjectName("horizontalSlider_3")
    self.verticalLayout_4.addWidget(self.horizontalSlider_3)
    self.horizontalLayout_3.addLayout(self.verticalLayout_4)
    self.verticalLayout.addLayout(self.horizontalLayout_3)
    self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_2.setObjectName("horizontalLayout_2")
    self.listWidget = QtWidgets.QListWidget(self.centralwidget)
    self.listWidget.setObjectName("listWidget")
    self.horizontalLayout_2.addWidget(self.listWidget)
    self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
    self.tabWidget.setObjectName("tabWidget")
    self.tab = QtWidgets.QWidget()
    self.tab.setObjectName("tab")
    self.tabWidget.addTab(self.tab, "")
    self.tab_2 = QtWidgets.QWidget()
    self.tab_2.setObjectName("tab_2")
    self.tabWidget.addTab(self.tab_2, "")
    self.horizontalLayout_2.addWidget(self.tabWidget)
    self.verticalLayout.addLayout(self.horizontalLayout_2)
    self.horizontalLayout = QtWidgets.QHBoxLayout()
    self.horizontalLayout.setObjectName("horizontalLayout")
    self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_2.setObjectName("pushButton_2")
    self.horizontalLayout.addWidget(self.pushButton_2)
    self.pushButton = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton.setObjectName("pushButton")
    self.horizontalLayout.addWidget(self.pushButton)
    self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_3.setObjectName("pushButton_3")
    self.horizontalLayout.addWidget(self.pushButton_3)
    spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
    self.horizontalLayout.addItem(spacerItem)
    self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
    self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
    self.horizontalSlider.setObjectName("horizontalSlider")
    self.horizontalLayout.addWidget(self.horizontalSlider)
    self.verticalLayout.addLayout(self.horizontalLayout)
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 1135, 23))
    self.menubar.setObjectName("menubar")
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

Solution

  • A QSplitter is a layout class and is available in Qt Designer.

    You need to replace the horizontal layout that currently contains the list-widget and tab-widget with a horizontal splitter. The steps to do that are:

    1. Right-click the main-window and choose Lay out -> Break layout from the menu.

    2. Right-click the tab-widget (or list-widget) and choose Lay out -> Break layout again.

    3. Select both the tab-widget and list-widget, and choose Lay out -> Lay Out Horizontally in Splitter from the right-click menu.

    4. Right-click the main-window and choose Lay out -> Lay Out Vertically from the menu.

    5. Click on the central-widget (in the Object Tree), then scroll down to the bottom of the Property Editor, and change layoutStretch to 0,1,0.