How to change splitter widgets programmatically in Qt?

I use QSplitter to place some widgets nearby.

As a user, I can resize these widgets by simply dragging the divider.

As a programmer, I don’t know how to specify exactly what width and what height I want at the moment.

This is my initial state (configured on different sites). The initial state

I tried using setFixedSize (), but after this call the user can no longer resize widgets (and this is definitely the right behavior because the size becomes "fixed"). enter image description here

If I use resize (), this has almost no effect. The widget changes, but (!) Incorrectly and (!), When I start to drag the widget again, it gets its initial state. enter image description here

Is there a way to resize the remaining widget in the code correctly? I do not want to have a fixed size, but resizing () does not work properly, as you can see. So what should I do?

+4
source share
1 answer

QSplitter has its own method QSplitter::setSizes(QList<int>) , where each entry in the list is the size of the widget in pixels, from left to right or from top to bottom, respectively. This method does not require you to know the exact width, it still works with guessed sizes.

I use this function, for example, to store custom sizes (obtained by QSplitter::sizes() ) in an instance of QSettings when the program shuts down and QSplitter::sizes() them when the software starts up again. If for some reason they are not installed, I just set the total width divided by the number of widgets in the splitter, and it works quite well as an initial state.

+7
source

All Articles