python-3.xpyqt5qsplitter

Concatenate Multiple QSplitter in PyQt5


I have 3 QWidgets which I want to concatenate. Basically it should look like this:

           |           |
           |           |
QWidget 1  | QWidget 2 | QWidget 3
           |           |
           |           |
       QSplitter1  QSplitter2

What I tried so far is:

1) Adding each widget to the corresponding QSplitter

widget1 = QWidget()
widget2 = QWidget()
widget3 = QWidget()

splitter1 = QSplitter(self)
splitter2 = QSplitter(self)

splitter1.addWidget(widget1)
splitter1.addWidget(widget2)

splitter2.addWidget(widget2)
splitter2.addWidget(widget3)

Here I get one moveable splitter, which should be splitter2

2) Adding the complete splitter1 Widget to splitter2 as first argument

widget1 = QWidget()
widget2 = QWidget()
widget3 = QWidget()

splitter1 = QSplitter(self)
splitter2 = QSplitter(self)

splitter1.addWidget(widget1)
splitter1.addWidget(widget2)

splitter2.addWidget(splitter1)
splitter2.addWidget(widget3)

Here only the first splitter is active.

What is the correct approach doing this?


Solution

  • QSplitter is a container widget, and it behaves similarly to a QBoxLayout, adding the possibility of resizing the items. This means that you (normally) only need one splitter, no matter how many widgets you are adding in the same orientation.

    What you are referring to as "splitters", are actually the splitter handles (QSplitterHandle classes), so the structure is actually this:

    QSplitter -------------------------------------+
    |               |               |              |
    h               H               H              |
    a               A               A              |
    n               N               N              |
    d   QWidget 1   D   QWidget 2   D  QWidget 3   |
    l               L               L              |
    e               E               E              |
    |               |               |              |
    +---------------+---------------+--------------+
    

    So, that's just as easy as this:

    widget1 = QWidget()
    widget2 = QWidget()
    widget3 = QWidget()
    
    splitter = QSplitter(self)
    splitter.addWidget(widget1)
    splitter.addWidget(widget2)
    splitter.addWidget(widget3)