Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does FlowLayout behave differently in Qt 5.2, compared to Qt 4.8?

I've just ported my application from Qt 4.8.4 to Qt 5.2.1. I have an issue with the FlowLayout class as provided in the FlowLayout example code in the Qt docs.

I have a QMainWindow with a QDockWidget docked at the bottom of the central widget. The QDockWidget has a FlowLayout with several child widgets. In Qt 4.8, this worked like a charm, the size of the child widgets fitted the standard size of the DockWidget. However, in Qt 5.2, the DockWidget tries to increase its size to the maximum (taking the place from the central widget). Changing its layout prevents this unwanted behavior. But of course, I use FlowLayout on purpose.

To illustrate the problem, I created a minimal example:

The constructor of the DockWidget:

    QGroupBox *generalBox = new QGroupBox("");
    generalBoxLayout = new FlowLayout;
    generalBox->setLayout(generalBoxLayout);

    for(int i=0; i<10; ++i)
    {
        QPushButton *button = new QPushButton("Test", this);
        button->setMinimumWidth(100);
        button->setMinimumHeight(100);
        generalBoxLayout->addWidget(button);
    }

    this->setWidget(generalBox);

Does someone know what the problem is and how I can solve it?

Edit I've created a new minimal working example and unfortunately cannot reproduce the discrepancy between Qt 4.8 and Qt 5.2. The same problem shows up in Qt 4.8, so I would still like to present it here:

Initial view showing the bottom-docked dockwidget taking the whole vertical space: Initial view showing the bottom-docked dockwidget

View after resizing the dockwidget with the mouse View after resizing the dockwidget with the mouse

View after resizing the mainwindow with the mouse View after resizing the mainwindow with the mouse

These screenshots show that the dockwidgets behaves as expected after changing the size of the dockwidget manually. However, on initialization, the widget takes all available space from central widget, which is not desired.

Does someone know of a solution / workaround?

like image 899
c_k Avatar asked Feb 18 '14 12:02

c_k


1 Answers

This is the answer given by Marek R. I'm putting it here so this question gets an answer. For too long it has been masquerading as an unanswered question.

I see the problem now. I did some experiments and research. It looks like some bug in Qt. FlowLayout::heightForWidth is always called with same width value (in my case 103) independently on main window size and this leads height bigger then desired (usually window is much wider). Also it is not called when width of main window is changing (it suppose to to update height of dock area).

like image 86
2 revs Avatar answered Dec 21 '22 08:12

2 revs