简体   繁体   English

如何在QPushButton中自动调整文本?

[英]How can i adjust text automatically in a QPushButton?

I have the following button 我有以下按钮

ui->setMaximumWidth(121);
ui->setMinimumWidth(121);
ui->setMaximumHeight(80);
ui->setMinimumHeight(80);
ui->pushButton->setStyleSheet   ("background-color: QLinearGradient(spread:pad x1: 0, y1: 0, x2: 0, y2: 1, stop: 0.0" + "#e5e4e2" + ",  stop: 0.4" + "#c0beb9" + ", stop: 1.0"+ "#e5e4e2"+");"
                                            "color: #868479; "
                                            "border-style: solid;"
                                            "border-style: solid;"
                                            "border-radius: 7;"
                                            "padding: 3px;"
                                            "padding-left: 5px;"
                                            "padding-right: 5px;"
                                            "border-color: #339;"
                                            "border-width: 1px;"
                                            "font:Bold;"
                                            "font-family:Georgia");

ui->pushButton->setText("Administración de Empresas");

But "Administración de Empresas" is a too long word then i cannot see the complete phrase. 但是“Administraciónde Empresas”这个词太长了,我看不到完整的短语。

Ps: i dont want to do it manually, i want that my app detect large phrases and adjust it automatically 附言:我不想手动进行操作,我希望我的应用能够检测到较大的词组并自动进行调整

It should be automatically fit to the text of push button. 它应该自动适合按钮的文本。

make sure you using QLayout in correct way. 确保您以正确的方式使用QLayout。

If you are manually setting the size in code, you can use the sizeHint property to get the right dimensions: 如果要在代码中手动设置大小,则可以使用sizeHint属性获取正确的尺寸:

button->resize(button->sizeHint().width(), button->sizeHint().height());

屏幕截图

Usually, it not good practic. 通常,这不是很好的实践。 But if you need to do word wrap on QPushButton, you can do it through QLayout: 但是,如果需要对QPushButton进行自动换行,可以通过QLayout进行:

QHBoxLayout *pLayout = new QHBoxLayout();
QLabel *pTextLabel = new QLabel();

pTextLabel->setText("This is a very very very long text");
pTextLabel->setAlignment(Qt::AlignCenter);
pTextLabel->setWordWrap(true);
pTextLabel->setTextInteractionFlags(Qt::NoTextInteraction);
pTextLabel->setMouseTracking(false);
pTextLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

pLayout->addWidget(pTextLabel);

MyButton->setText("");
MyButton->setLayout(pLayout);

But you have got a problem with automatic resize QButton height 但是你有一个自动调整QButton高度的问题

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

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