26

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?

c_k
  • 1,746
  • 1
  • 20
  • 35
  • 2
    I did try it with Qt 4.8.1, 5.0.1, 5.2.1 and I didn't noticed any difference. You need paste some screen shots to explain what is the problem. – Marek R Mar 06 '14 at 22:23
  • I have created a new minimal working example and embarrassingly enough, could not reproduce the original problem. To me, it is still unexpected behaviour though. I have extended my questions with screenshots. – c_k Mar 07 '14 at 12:08
  • 3
    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). – Marek R Mar 08 '14 at 21:41
  • In my case, the width value is 1072 (size of QMainWindow is 600x400 though). `FlowLayout::heightForWidth` is called two times, on initialization. On resizing the `QMainWindow` or `QDockWidget`, it is not called again. Seems like having `FlowLayout::hasHeightForWidth()` return false is a work-around for me. Not a great solution though. You are speaking about some bug, is it documented? – c_k Mar 10 '14 at 14:21
  • 6
    @MarekR You should add your comment as an answer and accept it. Otherwise this question is top in the list of unanswered qt5 questions, and as there are no answers (accepted or otherwise) people may not view this (and find the solution) – Bigwave Feb 19 '15 at 09:34

1 Answers1

4

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).

Community
  • 1
  • 1
Marcus
  • 1,685
  • 1
  • 18
  • 33
  • It answers first part of the question indeed. I never added it as an answer because the solution part was not answered. However I agree it makes sense to call this an answer now. – c_k Mar 22 '16 at 18:30
  • Has this bug been reported to the Qt team somewhere? – bparker May 07 '18 at 02:33