Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a QWidget's height before inserting

I have a widget to insert that looks like this

+--------------------+
| +-------++-------+ |
| |       ||       | |
| |       ||       | |
| |       || Label | |
| | Label || Label | |
| |       ||       | |
| |       ||       | |
| +-------++-------+ |
+--------------------+

A QWidget element containing two QVBoxLayouts, each containing one or more QLabels.
This Widget is going to be inserted in a Vertical Layout that has a stretch cell, so all the widgets of this kind will go to the top and shrink to the smaller height possible.

Resulting in something like this.

______________________ ← Layout boundary
+--------------------+
| +-------++-------+ |
| |       || Label | |
| | Label || Label | | ← Widget shrunk to the smallest
| +-------++-------+ |
+--------------------+
______________________ ← Layout boundary
          ↑
          |
          | ← Spacer
          |
          ↓
______________________ ← Layout boundary

But before doing this insert, I want to know the size that the widget would have in the inserted layout. Maybe the minimum size allowed by its internal layout. Not sure how to call it.

I have tried already many approaches like inserting the widget in an alternate invisible layout so I can retrieve its height when inserted, which should be the same that the widget would take when inserted in my definitive layout.

But for some reason I always get 480 or 478 when the widget is at most 50px height when pressed vertically. So I am totally lost.

I have to do this because I need the widget to animate when inserting, and I need to know the height it will take for me to animate from height 1 to it.

How can I do this?

like image 561
Ale Avatar asked Sep 16 '11 21:09

Ale


1 Answers

Without having the exact code, it's hard to say for sure, but having just attempted to reproduce your example, from the description above, I'd suggest looking at the following things:

  1. It sounds like you're actually calling widget->height() before the widget has been displayed. Instead, try calling widget->sizeHint().height() instead. The QWidget::sizeHint() method tells you what size the widget would like to be, if the parent geometry doesn't place any other constraints on it.

  2. If you're obtaining the sizeHint() and still getting the wrong answer, have a look at the documentation for QWidget::ensurePolished(). I believe that it's important to call this, to get more accurate geometries for widgets that have not yet been displayed:

    QWidget calls this function after it has been fully constructed but before it is shown the very first time. You can call this function if you want to ensure that the widget is polished before doing an operation, e.g., the correct font size might be needed in the widget's sizeHint() reimplementation.

  3. And if you're still having problems, then have a look at the documentation for QWidget::sizePolicy(), and the class QSizePolicy Class Reference. I must admit that however many times I read the docs on enum QSizePolicy::Policy, they never all sink in. It's really easy to have one incorrect size policy value mess up your layouts.

    It can be worth experimenting with the different size policies in Qt Designer, before actually applying a size policy.

like image 154
Clare Macrae Avatar answered Nov 12 '22 22:11

Clare Macrae