Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: How to resize a window to its new content

Tags:

qt

I have a window containing a QScrollArea with a couple widgets in it.

Until now, I was creating the QScrollArea and its child widgets in the constructor of my window, and then I was resizing the window vertically to fit its content using resize(400, sizeHint().height()). So far, so good.

Now, I'm adding or removing widgets in the QScrollArea at runtime. What should I do, after having added or removed widgets, to make the window fits its content vertically? Should I call adjustSize()? resize(sizeHint())? Should there be a call to layout->activate() or maybe updateGeometry() first? Which size policies actually matter in this case? The ones of the window, or of the scroll area, or both? I tried to set them all to Expanding.

I'm using Qt 4.6 on Windows.

like image 207
François Beaune Avatar asked Aug 31 '10 18:08

François Beaune


People also ask

How can you resize the program window?

Press Alt + Spacebar to open the window's menu. If the window is maximized, arrow down to Restore and press Enter . Press Alt + Spacebar again to open the window menu, arrow down to Size, and press Enter .

How do I resize a layout in Qt?

Once you have add your layout with at least one widget in it, select your window and click the "Update" button of QtDesigner. The interface will be resized at the most optimized size and your layout will fit the whole window. Then when resizing the window, the layout will be resized in the same way.

How do I move and resize a window?

Move or resize a window using only the keyboard. Press Alt + F7 to move a window or Alt + F8 to resize. Use the arrow keys to move or resize, then press Enter to finish, or press Esc to return to the original position and size. Maximize a window by dragging it to the top of the screen.


1 Answers

It seems that calling resize(sizeHint()) (without any other magic) after widgets were added to the scroll area actually does the trick. Somehow missed that the first time.

like image 147
François Beaune Avatar answered Sep 28 '22 10:09

François Beaune