Important: Please read the Qt Code of Conduct -

Add widget to layout with 0 weight/size

  • Is there an easy way to add a widget to a layout with 0 weight?

    My scenario: I want a title QLabel and a small "edit" button to the right of it. I want the title QLabel to be horizontally centered, with the edit button to be to the far right. If I use stretch factors, it still accounts for the edit buttons size, so the QLabel won't be truly centered.

  • I figured out a way to do it using QGridLayout since you can add widgets to the same cell.

    QLabel *title = new QLabel("Title", this);
    QLabel *edit = new QLabel("edit", this);
    QGridLayout *titleLayout = new QGridLayout;
    titleLayout->addWidget(title, 0, 0, Qt::AlignCenter);
    titleLayout->addWidget(edit, 0, 0, Qt::AlignRight);

Log in to reply