Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize splitter widgets programmatically in Qt?

I use QSplitter to place some widgets side by side.

Being a user, I can resize those widgets just dragging a splitter.

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

That's my original state (adjusted by different stretches). Original state

I tried to use setFixedSize(), but after that call the user can't resize widgets by itself anymore (and that's definitely correct behavior, because the size gets 'fixed'). enter image description here

If I use resize(), it has almost no effect. The widget is resized, but (!) incorrectly and (!) when I start dragging again the widget gets its initial state. enter image description here

Is there any way to resize that left widget in code correctly? I don't want to have fixed size but resize() doesn't work properly, as you can see. So what should I do?

like image 780
John Smith Avatar asked Aug 12 '15 15:08

John Smith


1 Answers

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

I use this functionality for instance to store the user defined sizes (obtained by QSplitter::sizes()) in a QSettings instance on the program shutdown and reapply them when the software is started again. If they are not set for some reason I just set the overall width divided by the number of widgets in the splitter and it works fine enough as an initial state.

like image 147
Bowdzone Avatar answered Nov 04 '22 20:11

Bowdzone