How can I stop a QPushButton from stretching vertically in a QHBoxLayout?
-
I am currently trying to make a header bar, that contains a few items- a logo, some space, a store button (with an icon), a
Sign Out
button, and finally a profile button. The store button should be 24px x 24px. I've stripped down my layout code to the following. Note the fixed size of the_storeButton
of 24px x 24px, and setting thesizePolicy
to fixed._layout = new QHBoxLayout(this); _layout->setContentsMargins(12, 12, 12, 12); _logo = new QLabel(this); std::string logoPath = imageBaseDir + "logo-full.png"; QPixmap logoPixmap = QPixmap(logoPath.c_str()); _logo->setPixmap(logoPixmap); _layout->addWidget(_logo); _layout->addStretch(); _storeButton = new QPushButton(this); _storeButton->setFixedSize(24, 24); _storeButton->setMaximumHeight(24); _storeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); _storeButton->setStyleSheet( "QPushButton {" "width: 24px;" "height: 24px;" "margin: 0px;" "border: 1px solid #FFFFFF;" "}"); _layout->addWidget(_storeButton); _logOutButton = new QPushButton(this); _logOutButton->setText("Sign Out"); _logOutButton->setStyleSheet( "QPushButton {font-size: 12pt; border-radius: 8px; padding: 8px; background-color: #232323; border: none; } " "QPushButton:hover {background-color: #444444; }"); _layout->addWidget(_logOutButton); auto avatar = new Avatar(this); _layout->addWidget(avatar);
The problem is that the
_storeButton
widget is being stretched to the height of all the other elements in theQHBoxLayout
:
(That empty white box should be 24px x 24px, but is clearly taller than 24px). Any ideas on why this is happening, and how I can force the button to be 24px x 24px? -
A clean project with above code will give a square button. I guess there's other stylesheet somewhere in its parent widget affecting it, such as min-height. If that's the case, just change the
height
in its stylesheet tomin-height
, that'll override the inherited property. -