Nominate our 2022 Qt Champions!

Align buttons on the right side of QHBoxLayout.

  • Hello!
    I need to place buttons on the horizontal layout, I have several buttons, one I want to put on the left size of layout and the rest on the right side. So I do

    //your code here

      lButtonLayout_ -> insertWidget(0, homeButton_);
      lButtonLayout_ -> insertWidget(1, backButton_);
      lButtonLayout_ -> insertWidget(2, addButton_);
      lButtonLayout_ -> insertWidget(3, removeButton_);
      lButtonLayout_ -> insertWidget(4, refreshButton_);
      lButtonLayout_ -> insertWidget(5, sendButton_, 1, Qt::AlignRight);
      rButtonLayout_ -> insertWidget(0, receiveButton_, 1);
      rButtonLayout_ -> insertWidget(1, rHomeButton_);
      rButtonLayout_ -> insertWidget(2, rBackButton_);
      rButtonLayout_ -> insertWidget(3, rAddButton_);
      rButtonLayout_ -> insertWidget(4, rRemoveButton_);
      rButtonLayout_ -> insertWidget(5, rRefreshButton_);
      buttonLayout_ -> insertLayout(0, lButtonLayout_);
      buttonLayout_ -> insertLayout(1, rButtonLayout_);
    on the left layout it is ok, but on the right layout the buttons which should be on the right corner are near the center, can you tell me what is wrong here?
    thank you.

  • Can you try a spacer to push them to your right

  • In the right layout all the items have the same default alignment, you never specified any specific alignment.

  • Lifetime Qt Champion

    @tokafr said:


    as @JohanSolo points out, you are not using any aligment which is extra paramter

  • I do
    insertWidget(1, rHomeButton_, 0, Qt::AlignRight);
    and so on for all buttons
    but it doesn't affect

Log in to reply