Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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();
        vboxLayout->addWidget(menu1);
        QHBoxLayout *hboxLayout = new QHBoxLayout();
        QToolButton *b1 = new QToolButton();
        QToolButton *b2 = new QToolButton();
        hboxLayout->addWidget(b1,Qt::AlignLeft);
        hboxLayout->addWidget(b2,Qt::AlignLeft);
        menu1->setLayout(hboxLayout);
        menu->setLayout(vboxLayout);
    
        QPalette pal = palette();
        // set black background
        pal.setColor(QPalette::Window, Qt::black);
        menu->setAutoFillBackground(true);
        menu->setPalette(pal);
    
        QPalette pal2 = palette();
        // set dark red background
        pal2.setColor(QPalette::Window, Qt::darkRed);
        menu1->setAutoFillBackground(true);
        menu1->setPalette(pal2);
    
    
    
        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);
        setLayout(layout);
    
        setWindowTitle(tr("Border Layout"));
    

    hbox align issue.png


  • Lifetime Qt Champion

    @jkwok678 Take a look at https://doc.qt.io/qt-5/qspaceritem.html
    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

    Hi,

    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