Felipe
Felipe

Reputation: 41

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.

Ps: i dont want to do it manually, i want that my app detect large phrases and adjust it automatically

Upvotes: 1

Views: 3228

Answers (2)

Keeper
Keeper

Reputation: 1

Usually, it not good practic. But if you need to do word wrap on QPushButton, you can do it through 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

Upvotes: 0

Farhad
Farhad

Reputation: 4181

It should be automatically fit to the text of push button.

make sure you using QLayout in correct way.

If you are manually setting the size in code, you can use the sizeHint property to get the right dimensions:

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

Screenshot

Upvotes: 1

Related Questions