Center a QLabel in a Qwidget
Unsolved
General and Desktop
-
I want to do like a toolbar, 3 buttons on the left and a label in the middle of the screen, no matter the size of my app.
I tried to do this with QToolBar but can't add a
QLabel
, so I'm trying with aQWidget
.My
QWidget
hasHorizontal Layout
, and I added 3QPushButton
and aQLabel
.How can I center the
QLabel
to always stay in the middle?I have tried different things, like putting the three buttons in a widget and the label in another widget and play with the layouts and size constrains but I haven't achieved it
-
Hi,
Out of curiosity, why not use the toolbar feature of QMainWindow for that ?
-
Okay! I research a little bit more and found what I was looking for. Example code:
QLabel* label = new QLabel("MY LABEL"); label->setAlignment(Qt::AlignCenter); QHBoxLayout* layout = new QHBoxLayout(); layout->addWidget(label, Qt::AlignCenter); QWidget* wid = new QWidget; wid->setContentsMargins(0, 0, 0, 0); wid->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); wid->setLayout(layout); QToolButton* p_butt_1 = new QToolButton; QToolButton* p_butt_2 = new QToolButton; QToolButton* p_butt_3 = new QToolButton; setStyleSheet("QToolButton { border: 1px solid magenta; }"); // Just for visibility in this example label->setStyleSheet("border: 1px solid blue;" "background-color: yellow;"); ui->mainToolBar->addWidget(p_butt_1); ui->mainToolBar->addWidget(p_butt_2); ui->mainToolBar->addWidget(wid); ui->mainToolBar->addWidget(p_butt_3);