2

I have a problem with size calculation in Qt when adding new widgets at run time. I have tried to illustrate the problem with the pruned program down there. Hopefully, it shows the same problem than my other (more complex) program:

When a widget has been shown, using:

w->show();

...and some new widgets are added to it later on (possibly w has a layout), what should I do to redisplay w and its parents so that the size of the newly added child widgets is taken into account? The behaviour I want is that the size recalculation propagates upward to the nearest scrolled object (or the main window if there is no scrolled objects)

In the example below, I create a little widget structure: if the call to show() (commented SHOW1) is removed, then the whole widget structure is defined before the first call to show() and everything works. BUT: if I do call show() (at SHOW1), then the last show does not display the right things: the frame is still too small

 #include <QApplication>
 #include <QtCore>
 #include <QMainWindow>
 #include <QTabWidget>
 #include <QWidget>
 #include <QGroupBox>
 #include <QVBoxLayout>
 #include <QLabel>

 #include <stdlib.h>


 int main(int argc, char *argv[])
 {
    QApplication app(argc, argv);
    QMainWindow* main_window = new(QMainWindow);
    main_window->setObjectName("main_window");
    main_window->resize(800, 600);
    main_window->setWindowTitle("Hello");

    QTabWidget* node_tab_widget = new QTabWidget(main_window);
    node_tab_widget->setObjectName(QString::fromUtf8("tab_widget"));

    QWidget* w= new QWidget(node_tab_widget);
    node_tab_widget->addTab(w, "TAB");

    QGroupBox* group_widget = new QGroupBox("GROUPNAME", w);
    QVBoxLayout*  group_layout = new QVBoxLayout;
    group_widget->setLayout(group_layout);
    group_layout->addWidget((QLabel*)new QLabel(">>>>>>>>>>>>>>>>>>>>>>>>>here1"));

    main_window->show();  // SHOW1: If this one is commented, then OK!

    group_layout->addWidget((QLabel*)new QLabel("here2"));
    group_layout->addWidget((QLabel*)new QLabel("here2"));
    group_layout->addWidget((QLabel*)new QLabel("here2"));
    group_layout->addWidget((QLabel*)new QLabel("here2"));

    main_window->setCentralWidget(node_tab_widget);

    // main_window->update();
    // main_window->hide();

    main_window->show(); // How to I get that to recaclulate the size of its contents?
    return app.exec();
 }
Samuel Harmer
  • 4,264
  • 5
  • 33
  • 67
user1159290
  • 951
  • 9
  • 27
  • I'll submit a more detailed answer in a bit. Just briefly though: I believe that the solution will rely on any child widgets which dictate the size of the parent being members of a layout inside that parent, and supplying a sensible [`minimumSize`](http://developer.qt.nokia.com/doc/qt-4.8/qwidget.html#minimumSize-prop) when requested by that layout. – sam-w Jan 20 '12 at 09:09
  • A minor point, but I would suggest you try to be consistent in your coding style. You've used three different forms of `new` in that code: `new(QMainWindow)`, `new QTabWidget(main_window)` and `new QVBoxLayout`. You might find [this question's](http://stackoverflow.com/q/1885849/594137) answers to be useful. – Samuel Harmer Jan 20 '12 at 10:26

1 Answers1

2

Your w widget doesn't have a layout. Try adding the following lines after creating group_widget:

QVBoxLayout *wlayout = new QVBoxLayout(w);
wlayout->addWidget(group_widget);
wlayout->addStretch();

You can get rid of addStretch if you actually want w to span the entire parent.

Paolo Capriotti
  • 4,052
  • 21
  • 25
  • Thanks, this did the job. Though it is not very clear to me why there is a need to have a layout on a widget containing another unique widget... I understood that layouts were for the organisation of children widgets, but obsviously, there is more things going on. you're welcome to explain if you have a minute. Many thanks, anyway. – user1159290 Jan 20 '12 at 11:43
  • If you have widget A contained in widget B without a layout, it just stays at coordinates 0,0 with whatever initial size it happens to be created with. Having a layout ensures that it is properly resized, that it has the correct margin, etc... – Paolo Capriotti Jan 20 '12 at 15:40