qt label alignment center
-
wrote on 12 Aug 2021, 08:58 last edited by
-
I put a label and a button on the vertical layout.
I want to put the label in the center, what should I do?wrote on 12 Aug 2021, 09:47 last edited by Qt embedded developer 8 Dec 2021, 10:03@IknowQT If possible please take horizontal layout there when you can add label and pushbutton at that time add layout stretch for your label.
-
wrote on 12 Aug 2021, 15:20 last edited by JoeCFD 8 Dec 2021, 15:25
create a horizontal layout. Align your label to the center and your button to the right. You may need some margin on the right side as well for your button.
-
wrote on 12 Aug 2021, 19:26 last edited by Cobra91151 8 Dec 2021, 19:26
Hello!
I suggest you check out the
setAlignment
(https://doc.qt.io/qt-5/qlayout.html#setAlignment) method and try it for your vertical layout. You can use theaddWidget
(https://doc.qt.io/qt-5/qboxlayout.html#addWidget) method and set the alignment specifically for your label, for example:Code:
setAlignment exampleQVBoxLayout *mainLayout = new QVBoxLayout(); mainLayout->setAlignment(Qt::AlignHCenter); mainLayout->addWidget(ui->label);
addWidget exmaple
QVBoxLayout *mainLayout = new QVBoxLayout(); mainLayout->addWidget(ui->label, 0, Qt::AlignHCenter);
Also, you can combine the vertical and horizontal layouts by using the
addLayout
(https://doc.qt.io/qt-5/qboxlayout.html#addLayout) method. In your question is unclear where the button should be placed, so please specify it and I will edit my code. Happy coding! -
I put a label and a button on the vertical layout.
I want to put the label in the center, what should I do?wrote on 13 Aug 2021, 05:23 last edited byAnother option to @Cobra91151 's answer is to use spacers.
Use spacers on all side and use
Grid
layout. -
@IknowQT If possible please take horizontal layout there when you can add label and pushbutton at that time add layout stretch for your label.
wrote on 23 Aug 2021, 07:14 last edited bythis->ui.horizontalLayout->setStretch(0, 10); this->ui.horizontalLayout->setStretch(1, 300); this->ui.horizontalLayout->setStretch(2, 10);
I changed it to a horizontal layout, but the middle is not aligned. The qt alignment is also centered.