0

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?

user984792
  • 53
  • 2
  • 7
  • Manual geometry management is exclusive of layout management. The `adjustSize` and `setGeometry` are no-ops when the widget's geometry is managed by a layout. Instead, you need to try to leverage the layout to manage your label in a manner you desire. Perhaps [this answer](http://stackoverflow.com/a/40874113/1329652), and the one it links to, might be relevant to your situation. – Kuba hasn't forgotten Monica Feb 17 '17 at 17:59
  • adjustSize() certainly does something here - without it, the qlabel retains its original size, allowing text to be cutoff in many cases. – user984792 Feb 19 '17 at 03:13
  • Please post a complete, self-contained single file example. It must be short - aim for <20 lines, begin with `#include `. For objects in a layout, `adjustSize()` is not supposed to do anything IIRC. I can't reproduce any of it. – Kuba hasn't forgotten Monica Feb 19 '17 at 16:14

0 Answers0