简体   繁体   English

在布局中设置QLabel上的文本不会调整大小

[英]Setting text on a QLabel in a layout, doesn't resize

Using the designer in Qt creator I have created a dialog that contains various widgets in a vertical layout. 在Qt创建器中使用设计器我创建了一个对话框,其中包含垂直布局中的各种小部件。 One of the widgets is a QLabel with word wrap set to true. 其中一个小部件是QLabel,其中自动换行设置为true。 The text for the QLabel is set just before the dialog is shown. QLabel的文本在对话框显示之前设置。

The maximum width and height of the QLabel is 16777215, the vertical size policy is set to Expanding and the horizontal is Preferred . QLabel的最大宽度和高度为16777215,垂直尺寸策略设置为“ 扩展” ,水平线为“ 首选” I've tried changing changing the size policy. 我已经尝试过更改大小政策。

The problem I have is that if the text is large, the QLabel fails to be adjusted accordingly and the text is clipped, like this: - 我遇到的问题是,如果文本很大,QLabel无法相应调整,文本被剪裁,如下所示: -

在此输入图像描述

I have tried calling updateGeometry() for the dialog, after setting the text and also tried calling update on the vertical layout, but nothing appears to make any difference. 我尝试在设置文本后调用updateGeometry(),并尝试在垂直布局上调用update,但似乎没有任何区别。 Ideally, I want the QLabel to adjust vertically to accommodate the text. 理想情况下,我希望QLabel垂直调整以适应文本。

Can someone tell me what I'm missing here? 谁能告诉我我在这里失踪了什么?

Set the vertical sizepolicy of your label to QSizePolicy::Minimum . 将标签的垂直sizepolicy设置为QSizePolicy::Minimum Then set the sizeconstraint of your dialog's layout to QLayout::SetMinimumSize . 然后将对话框布局的QLayout::SetMinimumSize设置为QLayout::SetMinimumSize This should make your dialog grow so all the content will fit inside of it. 这应该使您的对话框增长,以便所有内容都适合它。

Something like this: 像这样的东西:

QVBoxLayout *layout = new QVBoxLayout;
layout->setSizeConstraint(QLayout::SetMinimumSize);
this->setLayout(layout);
for(int i = 0; i < 5; i++)
{
    QLabel *label = new QLabel;
    label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
    label->setWordWrap(true);
    label->setText("This is a very long text. This is a very long text. This is a very long text. "
                   "This is a very long text. This is a very long text. This is a very long text. This is a very long text. "
                   "This is a very long text. This is a very long text.");
    layout->addWidget(label);
}

In my experiments, just setting the layoutSizeConstraint property to SetMinimumSize on the layout that contains the QLabel should be enough to let the label expand and adjust to its contents. 在我的实验中,只需在包含QLabel的布局layoutSizeConstraint属性设置为SetMinimumSize就足以让标签展开并调整其内容。

You can either change that property in Qt Designer, if you used it to build your UI, or via code: 您可以在Qt Designer中更改该属性,如果您使用它来构建UI,或者通过代码:

layout->setSizeConstraint(QLayout::SetMinimumSize);

Note that if you have nested layouts, you may need to set the constraint in all layouts up the chain. 请注意,如果您具有嵌套布局,则可能需要在链中的所有布局中设置约束。 No changes to the label's own sizePolicy is needed -- the defaults ( Preferred for both the horizontal and vertical size policy) should work, at least in my experience. 不需要更改标签自己的sizePolicy - 默认值(水平和垂直尺寸策略的Preferred )应该有效,至少在我的经验中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM