Qt adjustSize() fuzziness - what is it actually doing? - qt

I am running into a weird scenario where I don't understand the output of adjustSize(). I am calling adjustSize() on a QLabel, and the resulting size does not match the minimumSizeHint or the sizeHint - it is in between. The only other factor I can imagine is the length of the text inside the QLabel, but I get the same result for differing lengths, so that doesn't seem right either.
Here is the exact scenario:
I have a horizontal layout containing two QLabels.
One QLabel contains text and has a fixed width fw and minimum height mh. If all the text can fit within that size, the dimensions should be exactly fw x mh. If there is more text, the height should increase.
The second QLabel contains a pixmap and should always maintain its aspect ratio. The minimum width and minimum height exactly match the pixmap's original dimensions. If the height of the text-based QLabel increases, this second QLabel needs to increase in both width and height to match the text-based QLabel's new height while maintaining the aspect ratio of the pixmap.
This seems like it should be readily do-able. After the text of the first QLabel is changed (dynamically to any arbitrary length (within reason, not going outside the bounds of the screen or anything)), I call adjustSize() on that first QLabel. Then, based on the resulting height, I calculate the scale factor that the pixmap would have to increase by to match that height and reset the pixmap with scaled height and scaled width.
The problem is that the height of the text-based QLabel does not behave in any reasonable way I can discern. Even with drastically smaller text than what can fit in the starting size, adjustSize() still results in the height increasing past that starting minimum. Here I'm going to use some exact numbers to illustrate the issue. The minimum height is 385. The sizeHint says 401, and adjustSize results in 390. Why is the sizeHint 401 when the text can easily fit in the minimum of 385? And why is 390 chosen by adjustSize(), matching neither value?

Related

Enforce QWidget aspect ratio using sizeIncrement

I am designing a form in Qt Creator Designer. I want to make a QWidget that will always keep 4:3 aspect ratio and will keep the remaining space around itself empty.
I put it in a grid layout and surounded it with spacers which I thought would consume any extra space:
After that, I set it's minimum size to 48x32 and it's size increment to 2x3:
I tried all size policies but it never respects the aspect ratio. It also doesn't properly try to fit the whole area.
How do I set it up correctly?

Is it possible to give a size by ratio to qt designer ?

For exemple, I want the value of the layoutLeftMargin property to be equal to 1/3 of the parent widget. So when I will resize the windows, the ratio of the widget will still stay the same.
Else, if it's not possible with QtDesigner, how can I do it with code ?
No, Margins are specified in pixels, they can't be relative to the parent widget's size.
However, You can do that in the designer by putting your whole current layout in a Horizontal Layout, add a Horizontal Spacer to the left of it, assign suitable layoutStretch values in the horizontal layout (In your example, this should be 1,2, meaning the original layout will take up twice the space taken up by the spacer, so that the spacer gets 1/3 of the parent widget).

Can someone explain size hint, size policy, size constraint in QT?

Can anyone give a clear explain of these 3 concept? What's the difference and how to use them?
size hint is the preferred size of the widget, layouts will try to keep it as close to this as possible.
size policy describes how the size may change when the preferred size cannot be used (can it stretch or shrink) see the QSizePolicy::Policy enum for a description of each.
size constraint are the maximumSize and minimumSize the widget can be.
It puzzles me a lot too until I understand the Layout Management of Qt. According to its docs:
When you add widgets to a layout, the layout process works as follows:
All the widgets will initially be allocated an amount of space in accordance with their QWidget::sizePolicy() and QWidget::sizeHint().
If any of the widgets have stretch factors set, with a value greater than zero, then they are allocated space in proportion to their stretch factor (explained below).
If any of the widgets have stretch factors set to zero they will only get more space if no other widgets want the space. Of these, space is allocated to widgets with an Expanding size policy first.
Any widgets that are allocated less space than their minimum size (or minimum size hint if no minimum size is specified) are allocated this minimum size they require. (Widgets don't have to have a minimum size or minimum size hint in which case the stretch factor is their determining factor.)
Any widgets that are allocated more space than their maximum size are allocated the maximum size space they require. (Widgets do not have to have a maximum size in which case the stretch factor is their determining factor.)
And sizeHint() is the recommended size of a QWidget, and the Layout of the widget parent will take sizeHint() and sizePolicy() into consideration to determine the space of the child widget can hold.

Enable QLabel to shrink even if it truncates text

How can I get a QLabel to be resized even if it means truncating its containing text? I have a QLabel stretching the whole horizontal space of a Widget. When setting its text I make sure it is correctly truncated, ie getting its FontMetrics and Width and using metrics.elidedText().
But when the user resizes the widget the Label doesn't allow it to shrink any further since it would truncate its text.
Any ideas how to solve this? The simplest solution I think would be to somehow tell the QLabel to always shrink and then catch the resize event and correctly format the text - I just have no idea how to do the first part (different size policies don't help)
Although you mention that setting size policies didn't help, setting the QLabel's horizontal size policy to QSizePolicy::Ignored should tell the containing layout manager to ignore any minimum size hint from the label. An alternative would be to set the QLabel's minimum horizontal size to a non-zero value, like 1. If neither of these work then there is something else interfering.

Widgets next to each other same size in Qt

I have two widgets, one arbitrary (usually a QLineEdit), and one QLabel which displays a Pixmap. They are placed next to each other with a QHBoxLayout. The widget with this layout is in turn placed in another layout.
Now, what I want is that the label with the pixmap is automatically resized so that it is as high as the arbitrary widget next to it. However, even when I set the label's sizePolicy to Maximum, it still seems to expand to the original pixmap size, instead of resizing the pixmap and shrinking to match the other widget. Instead of having two equally large widgets I have the arbitrary one which is smaller than the pixmap next to it.
Any ideas how to get the size of the pixmap label to match the size of the widget next to it?
How about:
int height = arbitraryWidget->height(); // get desired height.
label->setSizeHint(QSize(label->width(), height); // set size hint to current width and desired height.
label->setSizePolicy(QSizePolicy::Fixed); // optional, but this ensures size is desired.

Resources