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



  •     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?


  • Moderators

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.