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
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 611 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 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
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on 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 last edited by
        #3

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

        1 Reply Last reply
        1

        • Login

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