Struggling with centering a QLabel
vertically in a QHBoxLayout
. Here's the relevant part of my code:
QFrame* topBar = new QFrame();
topBar->setStyleSheet("background-color: #2c3d50;border-bottom: 3px solid #2c92b6;");
topBar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
topBar->setFixedHeight(24);
QHBoxLayout* topBarLayout = new QHBoxLayout();
QLabel* label = new QLabel("MSFT");
label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
label->setStyleSheet("font-size: 15px;font-weight: bold;border: 0px;");
label->setMinimumHeight(15);
topBarLayout->addWidget(label);
topBar->setLayout(topBarLayout);
topLayout->addWidget(topBar);
My aim is to vertically center the label. Attempts so far:
Qt::AlignVCenter
- no effectQSizePolicy::PreferredSize
andQSizePolicy::Expanding
made label shrink without height adjustments- Tweaking
minimumHeight
andpadding/margin
only moved the label downwards
https://i.sstatic.net/EjbK0.png
Goal is a fixed height bar with labels and buttons aligned on both sides regardless of its width.
Suggestions include adding another QBoxLayout
for vertical alignment, but this seems impractical with numerous components.
TL;DR: How to align a Label
text vertically within a horizontal layout?