1

When the application is launched but yet in construction time (when all the initial widget is being created and so on), I add to my layout a QSplitter, with two widgets, and I want the second widget occupies more or less the 60/70% of the available space, while the first widget takes the rest of the space (taking into account the splitter size itself).

So, in the beginning, before the windows itself is shown, I try to readjust these two widget more or less like that:

splitter->setOrientation(Qt::Vertical);
auto sizes = splitter->sizes();

int& first_w = sizes.front(); // == 0
int& second_w = sizes.last(); // == 0
int diff = form_w * 2.3 - tbl_w; (30% * 2.3 = ~70%)

second_w += diff;
first_w -= diff;

splitter->setSizes(sizes);

But, first_w and second_w contains 0 and 0, I don't know if because the sizes have not been calculated yet, or because the widget is not yet shown (the window isn't) and the sizes of invisibles widgets are 0.

What can I do to get the "future" sizes of these widgets? The splitter is inside a QVBoxLayout, and the available space of the owner widget is more or less all the vertical space of the window (there's only a QTabBar above the widget owning that QVBoxLayout, and a bit of padding in the window).

ABu
  • 10,423
  • 6
  • 52
  • 103

2 Answers2

1

According to the docs the following should do the right thing...

splitter->setSizes(QList<int>() << 30 << 70);

If the QSplitter is vertical that should apportion 30% to the top widget and 70% to the lower.

G.M.
  • 12,232
  • 2
  • 15
  • 18
  • 1
    No, it doesn't work (I have test that). That values set the sizes in pixels I think, and since there's a lot of free space to be distributed among them, that values are completely replaced by the `sizeHint`s. The offending widget is a `QTextEdit` that there's inside the form which takes too much vertical space. – ABu Dec 13 '16 at 18:56
0

The method QSplitter::sizes() seems to give non-zero sizes only when the widget is shown. What I usually do to circumvent this problem is override QWidget::showEvent(QShowEvent *event) and invoke the method QSplitter::setSizes() there.

piarston
  • 1,685
  • 1
  • 13
  • 25
  • That's right!! I didn't think about it. But I think it would be a better aproach to connect a slot to the `shown` signal, but, oh!! There's no `shown` signal! – ABu Dec 14 '16 at 17:28