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. Keep widget centered in QHBoxLayout after adding another widget to the edge
Forum Update on Monday, May 27th 2025

Keep widget centered in QHBoxLayout after adding another widget to the edge

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 619 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.
  • T Offline
    T Offline
    tjudg
    wrote on 29 Jan 2017, 04:43 last edited by
    #1
        QHBoxLayout *layout = new QHBoxLayout();
        QLabel *label = new QLabel();
        label->setText("label");
        layout->addStretch();
        layout->addWidget(label);
        layout->addStretch();
        QPushButton *button = new QPushButton();
        layout->addWidget(button);
        setMinimumSize(500,100);
        setLayout(layout);
    

    alt text
    alt text

    If I only add a QLabel to the layout then it is centered but when I try to add a QPushButton to the right side, the label moves a bit to the left. Is there a way to keep the label in in the same position as the first image after I add the button?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 29 Jan 2017, 05:20 last edited by
      #2

      @tjudg Perhaps you should try adding a horizontal QSpacerItem on the left side.

      157

      1 Reply Last reply
      1
      • T Offline
        T Offline
        tjudg
        wrote on 29 Jan 2017, 05:40 last edited by
        #3

        Thanks, I used the addSpacing(int) method and it worked.

        1 Reply Last reply
        1

        2/3

        29 Jan 2017, 05:20

        • Login

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