gpt4 book ai didi

QT Flowlayout - 多行标签问题

转载 作者:行者123 更新时间:2023-12-02 04:22:38 25 4
gpt4 key购买 nike

我有一个关于 QT 布局的小问题。

我有一个工具箱,我想用一些带有说明的可检查按钮来填充它。所以我创建了一个带有 QGridLayoutQWidget 并将 QButton 放在第一个单元格中,将 QLabel 放在第二个单元格中.

这是代码中最重要的部分(我删除了应用中其他不相关代码的依赖项):

QWidget *createCellWidget()
{
QToolButton *button = new QToolButton(this);
button->setCheckable(true);
button->setMinimumSize(57,57);
button->setMaximumSize(57,57);

QWidget *widget = new QWidget(this);
QGridLayout *layout = new QGridLayout(widget);
layout->addWidget(button, 0, 0, Qt::AlignHCenter);
QLabel *lbl = new QLabel("my very long caption");
lbl->setWordWrap(true);
lbl->setAlignment(Qt::AlignCenter);

layout->addWidget(lbl, 1, 0, Qt::AlignTop);
widget->setMaximumWidth(80);
widget->setMinimumWidth(80);

return widget;
}

然后我创建一个 QGridLayout 并用这些控件填充它:

QWidget *itemWidget_FlowControl = new QWidget(this);
QGridLayout *_flowControl_layout = new QGridLayout(itemWidget_FlowControl);
_flowControl_layout->addWidget(createCellWidget(), 0, 0);

这很好用并产生了这个输出:

Widgets properly aligned

这是一个很好的布局。不幸的是,如果我放大窗口,控件不会“流动”,所以我尝试用 flowlayout ( here are the source files ) 替换 QGridLayout。

现在行为好多了。但是...

Widgets not alignet

这就是我得到的。较长的标题的布局就好像它们是单行的,因此文本与按钮重叠。

我该怎么做才能使它看起来像以前一样,但要保持“流式布局”?或者您知道 QT 5.2 中是否有任何替代方案?

谢谢

最佳答案

怎么了?

您在使用流程布局方面走在正确的轨道上。

唯一的问题是您的单元格小部件的内部布局(在您的情况下为QGridLayout)也会根据调整大小事件进行拉伸(stretch)。


解决方案

解决方案非常简单:

Try to limit the stretch of the internal layout.

在工厂函数 QWidget *createCellWidget() 中:

[选项 1]

添加lbl->setMaximumWidth(60);来手动限制标签宽度的拉伸(stretch)。这使得内部布局不那么“自由”地拉伸(stretch)。

[选项 2]

添加layout->setSizeConstraint(QLayout::SetFixedSize);来限制内部布局拉伸(stretch)。通过这样做,您可能需要手动添加一些换行代码 (\n) 到您的“超长标题”,以防 Qt 自动确定的标签宽度不适合您的需要。


结果

enter image description here

关于QT Flowlayout - 多行标签问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29237206/

25 4 0