与Qt 4.8相比,为什么FlowLayout在Qt 5.2中的表现不同?

时间:2022-09-10 06:36:55

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.

我刚刚将我的应用程序从Qt 4.8.4移植到Qt 5.2.1。我在Qt文档中的FlowLayout示例代码中提供了FlowLayout类的问题。

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.

我有一个QMainWindow,QDockWidget停靠在*小部件的底部。 QDockWidget有一个带有几个子窗口小部件的FlowLayout。在Qt 4.8中,这就像一个魅力,子小部件的大小符合DockWidget的标准大小。但是,在Qt 5.2中,DockWidget尝试将其大小增加到最大值(从*窗口小部件取而代之)。更改其布局可防止此不良行为。但当然,我故意使用FlowLayout。

To illustrate the problem, I created a minimal example:

为了说明这个问题,我创建了一个最小的例子:

The constructor of the DockWidget:

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:

编辑我创建了一个新的最小工作示例,遗憾的是无法重现Qt 4.8和Qt 5.2之间的差异。同样的问题出现在Qt 4.8中,所以我仍然想在这里介绍它:

Initial view showing the bottom-docked dockwidget taking the whole vertical space: 与Qt 4.8相比,为什么FlowLayout在Qt 5.2中的表现不同?

显示底部停靠的dockwidget占据整个垂直空间的初始视图:

View after resizing the dockwidget with the mouse 与Qt 4.8相比,为什么FlowLayout在Qt 5.2中的表现不同?

使用鼠标调整dockwidget的大小后查看

View after resizing the mainwindow with the mouse 与Qt 4.8相比,为什么FlowLayout在Qt 5.2中的表现不同?

使用鼠标调整主窗口大小后查看

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.

这些屏幕截图显示,在手动更改dockwidget的大小后,dockwidgets的行为与预期一致。但是,在初始化时,窗口小部件从*窗口小部件获取所有可用空间,这是不希望的。

Does someone know of a solution / workaround?

有人知道解决方案/解决方法吗?

1 个解决方案

#1


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.

这是Marek R.给出的答案。我把它放在这里,所以这个问题得到了答案。长久以来,它一直伪装成一个悬而未决的问题。

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

我现在看到了问题。我做了一些实验和研究。它看起来像Qt中的一些错误。 FlowLayout :: heightForWidth总是在主窗口大小上独立调用具有相同宽度值(在我的情况下为103),这导致高度大于所需(通常窗口更宽)。当主窗口的宽度发生变化时也不会调用它(它假设要更新停靠区域的高度)。

#1


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.

这是Marek R.给出的答案。我把它放在这里,所以这个问题得到了答案。长久以来,它一直伪装成一个悬而未决的问题。

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

我现在看到了问题。我做了一些实验和研究。它看起来像Qt中的一些错误。 FlowLayout :: heightForWidth总是在主窗口大小上独立调用具有相同宽度值(在我的情况下为103),这导致高度大于所需(通常窗口更宽)。当主窗口的宽度发生变化时也不会调用它(它假设要更新停靠区域的高度)。