PyQt QSplitter

QSplitter

The QSplitor class implements splitter widgets. The splitter allows you to resize the child widget by dragging the boundary. Let's use the example to split the widget into four smaller areas.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QFrame, QSplitter
from PyQt5.QtCore import Qt

class MyApp(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        hbox = QHBoxLayout()

        top = QFrame()
        top.setFrameShape(QFrame.Box)

        midleft = QFrame()
        midleft.setFrameShape(QFrame.StyledPanel)

        midright = QFrame()
        midright.setFrameShape(QFrame.Panel)

        bottom = QFrame()
        bottom.setFrameShape(QFrame.WinPanel)
        bottom.setFrameShadow(QFrame.Sunken)

        splitter1 = QSplitter(Qt.Horizontal)
        splitter1.addWidget(midleft)
        splitter1.addWidget(midright)

        splitter2 = QSplitter(Qt.Vertical)
        splitter2.addWidget(top)
        splitter2.addWidget(splitter1)
        splitter2.addWidget(bottom)

        hbox.addWidget(splitter2)
        self.setLayout(hbox)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QSplitter')
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

The window is divided into four small areas. You can style frames in each area differently.

qsplitter

Related course: Create Desktop Apps with Python PyQt5

Description

top = QFrame()
top.setFrameShape(QFrame.Box)

midleft = QFrame()
midleft.setFrameShape(QFrame.StyledPanel)

midright = QFrame()
midright.setFrameShape(QFrame.Panel)

bottom = QFrame()
bottom.setFrameShape(QFrame.WinPanel)
bottom.setFrameShadow(QFrame.Sunken)

Create a frame to fit in each area. You can set the shape of the frame and the style of the shadow using setFrameShape and setFrameShadow.

As in the example, you can use the NoFrame/Box/Panel/StyledPanel/HLine/VLine/WinPanel for setFrameShape and the Plain/Rained/Sunken constant for setFrameShadow.

See the image below for a description and results of the use of each constant.

A constant that sets the shape of a frame.

A constant that sets the shadow of a frame.

Shape and shadow of the frame

splitter1 = QSplitter(Qt.Horizontal)
splitter1.addWidget(midleft)
splitter1.addWidget(midright)

splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(top)
splitter2.addWidget(splitter1)
splitter2.addWidget(bottom)

Use QSplitter to split horizontally, with the midleft on the left and the midright frame widget on the right.

Next, split it vertically and add three frame widgets: top, splitter1, and bottom.

hbox.addWidget(splitter2)
self.setLayout(hbox)

Put the splitter2 widget in a horizontal box layout and set it to full layout.