qt label alignment center
Solved
General and Desktop
-
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);
Result:
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! -
this->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.