Qt: How to resize a window to its new content - 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.

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.

Related

Getting QScrollArea to resize its child widget immediately

I have a QScrollArea (we'll call it myContainer) that contains and scrolls a child view (we'll call it myChildWidget). All works almost fine - when I change the height of myChildWidget dynamically in response to something, if the height exceeds that of myContainer (the QScrollArea), a vertical scroll bar pops into view, effectively narrowing myChildWidget since I've set it to resize its child using:
myContainer->setWidgetResizable(true);
The problem is that myChildWidget is not actually resized until later, rather than right when I set its new height or try resizing myContainer, so I can't do certain things depending on its new width without subclassing and putting in a whole bunch of extra code. Surely there's something I can call to get the QScrollArea to auto-resize its child right away, right? I can use:
QCoreApplication::processEvents();
but the problem with that is that it causes the widgets to flash and redraw when I'm not done setting things up. I've tried update(), updateGeometry(), and adjustSize(), both on the container and its child, and none work. Anything I'm missing? Thanks for any help.

QT layout manager does not recover space when hiding widget until window is moved

I am using Qlabels to plot some graphs and images (via setpixmap).
My basic layout is:
QVBoxlayout main layout via qdialog's setlayout.
QHboxlayout (array of QLabels)
Qlabel expandedPlot (optional expanded plot of one of the above QLabels)
QLabel mainImage Main image display
Within the QDialogs re-implemented keypress event handler, I hide()/show() the expanded plot. When I hide() the expandedPlot, the layoutmanager recovers about 1/2 of the vertical usage. Then when I drag the window, the layout manager recovers the remainder of the vertical space (as if there was no item present).
How can I force the behavior of moving the window? I want the layout manager to completely recover the vertical space.
I am using Qt 5.6 on windows, but want cross-platform solutions.
Thank you, mike
Because laying out the widget is quite expensive, Qt doesn't always do it. If you change the size of enclosed widgets you are likely to need to call updateGeometry on them to trigger the enclosed layout manager to re-layout. But if you hide the widget, updateGeometry does nothing. In that case you need to call adjustSize on the parent widget, which will then trigger the re-layout.

Why has my QWidget stopped expanding to fit its children?

Here is a screenshot of my Qt application. I have blurred everything which isn't important. Look at the bottom-right.
That "Comment" bit is a QGroupBox containing a QTextEdit, it is being cropped because some containing QWidget is not expanding to fit it.
So far I have seen widgets expand by default to fit their contents (as long as child widgets are added to layouts before they become visible, which is what's happening here) but suddenly this is no longer happening. If I manually expand the window, the "Comment" box is laid out as expected.
The innermost QTextEdit has setMinimumHeight called and I have verified that every widget in the hierarchy has QSizePolicy::GrowFlag set in its sizePolicy. I have grepped the source to check that I am not setting maximum sizes anywhere in the application. And, yes, the QTextEdit is in the layout of the QGroupBox.
It seems like Qt is saying "I'll automatically expand the main window to fit the contents, but only up to a limit."
Why is this happening and how do I stop it? Or rather, how do I make it continue the helpful auto-resizing behaviour regardless of the size/depth of child widgets?

How to set the desired size of a QMainWindow with QScrollArea?

I am working on a project where I have to display a pretty large (vertically) main Widget.
In the initial Version of my GUI it was just added as the central Widget of a QMainWindow, which caused the Problem that on small screen resolutions the controls on the Bottom of the Widget are unreachable.
To solve this i wrapped a QScrollArea around the main Widget, but now the main window is always relatively small even if it doesn't have to.
What do i need to change so the Main Windows (vertical) size is large enough to show all the contents unless it would be too large for the screen resolution? Also I don't want it to be stretched, so simply always using the whole vertical screen resolution is not an option. Ideally the size should be fixed to the size needed by the contents (w/o the scroll area) and only smaller where needed.
Overriding the sizeHint method did only resulted in a small enlargement of the Window and setting the minimal height brings me back to the beginning where some of the controls are not assessable on small resolutions.
Since i am new to QT I am actually out of ideas how to google the solution because most Solutions I can find are about sizing components inside a Window and not the Window itself.
By default a QScrollArea will not attempt to expand to fit its contents. In order to do this you will need to re-implement QScrollArea's sizeHint() to return the size of QScrollArea's child widgets.
In your question it sounds like you were trying to re-implement MainWindow's sizeHint? re-implementing sizeHint on the top-level window will have no effect as sizeHint designed for use with widgets inside layouts.

QT Layout - initial directions

I am new to QT. I'm trying to understand the layout mechanism by trying to implement this small window seen below. It has the following elements under the QWidget that's the main window:
One big QWidget that stretches on all the client area.
Two QWidget containers on the top of the window. Both should have the same height, but the right one stretches horizontally, as the window grows/shrinks.
one button container widget on the top right, with fixed height and width
Large QWidget container filling the rest of the client area, that should resize as the window resizes.
The parent window itself is resizeable.
I'm looking for hints as to what layout I should use. How do I achieve this programatically? define what stretches automatically, what stays with a fix size? and how the proportions are kept where they need to be kept.
I'd appreciate any pointer you may have.
The easiest, and IMHO best, way to accomplish this is via the QHBoxLayout and QVBoxLayouts. You can do this via the designer in QtCreator, but I find it doesn't work perfectly if you need to adapt things over time. If it's a static set of widgets, I do suggest designing it using the QtCreator designer as it'll greatly simplify your life.
If you're going to do it programatically, the main window should be set to use a QVBoxLayout and then two sub-QVBoxLayout's after that, where the bottom one is configured to take any space it can get. Then in the top QVBoxLayout, add a QHBoxLayout with your two upper components.
to set a widget to fixed size in code you call setFixedSize( int h, int w ) on the widget. To do it in Designer click on the widget and look in the property editor in the QWidget section. open the sizePolicy thingy and set horizontal and/or vertical to fixed. Then open Geometry and set the width and Height.
To make them stretch at different ratios in code you use a separate argument when using a box layout. eg layout->addWidget( button1, 1 ); layout->addWidget (button2, 2); this would cause button2 to expand at twice the rate of button1. To do this in designer, open the sizePolicy property of the widgets and set the HorizontalStrech and/or VerticalSretch. Note that the size policy needs to not be Fixed in this case for the direction you want to set the stretch on. Also it will never let a widget shrink below its minimum size (it would rather mess up the ratio than shrink something too small).

Resources