在布局中的QLabel上设置文本,不会调整大小 [英] Setting text on a QLabel in a layout, doesn't resize

查看:1263
本文介绍了在布局中的QLabel上设置文本,不会调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Qt Creator中的设计器,我创建了一个对话框,其中包含垂直布局的各种小部件.小部件之一是QLabel,其自动换行设置为true.在显示对话框之前设置QLabel的文本.

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.

QLabel的最大宽度和高度为16777215,垂直尺寸策略设置为扩展,水平尺寸为首选.我尝试更改尺寸政策.

The maximum width and height of the QLabel is 16777215, the vertical size policy is set to Expanding and the horizontal is Preferred. I've tried changing changing the size policy.

我的问题是,如果文本很大,则QLabel无法相应地进行调整,并且文本会被剪切,如下所示:-

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: -

在设置文本之后,我尝试为对话框调用updateGeometry(),还尝试在垂直布局上调用update,但是似乎没有任何区别.理想情况下,我希望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.

有人可以告诉我我在这里想念什么吗?

Can someone tell me what I'm missing here?

推荐答案

将标签的垂直大小策略设置为QSizePolicy::Minimum. 然后将对话框的布局的大小约束设置为QLayout::SetMinimumSize. 这应该使您的对话框扩大,以便所有内容都适合其中.

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.

类似这样的东西:

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);
}

这篇关于在布局中的QLabel上设置文本,不会调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆