Nominate our 2022 Qt Champions!

HBoxlayout items starting on the left?

  • I've been trying to use the HBoxlayout and I'm trying to get all the QToolButtons to start and clump on the left hand side (unlike the screenshot). I've been adding them to the HBoxLayout with Qt::alignLeft. What am I doing wrong?

     QTextBrowser *centralWidget = new QTextBrowser;
        centralWidget->setPlainText(tr("Central widget"));
        BorderLayout *layout = new BorderLayout;
        QWidget *menu = new QWidget();
        QVBoxLayout *vboxLayout = new QVBoxLayout();
        QWidget *menu1 = new QWidget();
        QHBoxLayout *hboxLayout = new QHBoxLayout();
        QToolButton *b1 = new QToolButton();
        QToolButton *b2 = new QToolButton();
        QPalette pal = palette();
        // set black background
        pal.setColor(QPalette::Window, Qt::black);
        QPalette pal2 = palette();
        // set dark red background
        pal2.setColor(QPalette::Window, Qt::darkRed);
        layout->addWidget(centralWidget, BorderLayout::Center);
        layout->addWidget(menu, BorderLayout::North);
        layout->addWidget(createLabel("West"), BorderLayout::West);
        layout->addWidget(createLabel("East 1"), BorderLayout::East);
        layout->addWidget(createLabel("East 2") , BorderLayout::East);
        layout->addWidget(createLabel("South"), BorderLayout::South);
        setWindowTitle(tr("Border Layout"));

    hbox align issue.png

  • Lifetime Qt Champion

    @jkwok678 Take a look at
    Especially "QHBoxLayout addSpacing(), addStretch(), insertSpacing(), insertStretch()"

  • I'm just wondering if this is the only way to do it? Is there a more elegant way to do it so that it can work with a GUI that changes when the window changes size?

  • Lifetime Qt Champion


    What changes do you have in mind ?

  • For example what happens with the spacers, when the window size isn't big enough to justify them being there, as in it distorts the other elements in the GUI?

  • @jkwok678
    The spacers are just suitable for a changing window size.
    When there's no enough space, their size will shrink to zero.

  • So I'm just wondering is there any point using Qt::Align___?

Log in to reply