C++ 在布局中的 QLabel 上设置文本,不调整大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19293507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting text on a QLabel in a layout, doesn't resize
提问by TheDarkKnight
Using the designer in Qt creator I have created a dialog that contains various widgets in a vertical layout. One of the widgets is a QLabel with word wrap set to true. The text for the QLabel is set just before the dialog is shown.
使用 Qt creator 中的设计器我创建了一个对话框,其中包含垂直布局的各种小部件。小部件之一是自动换行设置为 true 的 QLabel。QLabel 的文本是在显示对话框之前设置的。
The maximum width and height of the QLabel is 16777215, the vertical size policy is set to Expandingand the horizontal is Preferred. I've tried changing changing the size policy.
QLabel 的最大宽度和高度为 16777215,垂直大小策略设置为Expanding,水平为Preferred。我试过改变尺寸政策。
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. Ideally, I want the QLabel to adjust vertically to accommodate the text.
在设置文本后,我尝试为对话框调用 updateGeometry(),并尝试在垂直布局上调用 update,但似乎没有任何区别。理想情况下,我希望 QLabel 垂直调整以适应文本。
Can someone tell me what I'm missing here?
有人可以告诉我我在这里缺少什么吗?
回答by thuga
Set the vertical sizepolicy of your label to QSizePolicy::Minimum
.
Then set the sizeconstraint of your dialog's layout to QLayout::SetMinimumSize
.
This should make your dialog grow so all the content will fit inside of it.
将标签的垂直尺寸策略设置为QSizePolicy::Minimum
。然后将对话框布局的 sizeconstraint 设置为QLayout::SetMinimumSize
. 这应该会使您的对话框增长,以便所有内容都适合其中。
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);
}
回答by waldyrious
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
水平和垂直尺寸策略)应该有效,至少在我的经验中是这样。