Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I stop a QPushButton from stretching vertically in a QHBoxLayout?

How can I stop a QPushButton from stretching vertically in a QHBoxLayout?

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++layout issuesstretch
3 Posts 2 Posters 251 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MarkLT1
    wrote on 11 Nov 2024, 21:19 last edited by
    #1

    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 the sizePolicy 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 the QHBoxLayout:
    10be38a5-55b8-45cb-8fa0-bd6c11d30ee5-image.png
    (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?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on 12 Nov 2024, 01:43 last edited by Bonnie 11 Dec 2024, 01:48
      #2

      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 to min-height, that'll override the inherited property.

      M 1 Reply Last reply 12 Nov 2024, 08:30
      2
      • B Bonnie
        12 Nov 2024, 01:43

        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 to min-height, that'll override the inherited property.

        M Offline
        M Offline
        MarkLT1
        wrote on 12 Nov 2024, 08:30 last edited by
        #3

        @Bonnie Thank you for pointing me in the right direction. I turns out the issue wasn't height, or min-height, but rather an inherited padding, that just happened to make it look like it was expanding to the full height of the QHBoxLayout.

        1 Reply Last reply
        0

        1/3

        11 Nov 2024, 21:19

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved