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

QGroupBox not fitted to contents



  • Hi, I've wrote this code:

        QVBoxLayout *VLayout = new QVBoxLayout(this);
        QHBoxLayout *hlButtons = new QHBoxLayout;
    
        //Tabs
        QGroupBox *groupTabs = new QGroupBox("Tabs");
        QPushButton *btnNewTab = new QPushButton("New Tab");
        QPushButton *btnRenameTab = new QPushButton("Rename Tab");
    
        QVBoxLayout *vlTabs = new QVBoxLayout;
        vlTabs->addWidget(btnNewTab);
        vlTabs->addWidget(btnRenameTab);
        
        vlTabs->addStretch(true);
        hlButtons->addWidget(groupTabs);
        groupTabs->setLayout(vlTabs);
    
        VLayout->addLayout(hlButtons);
    

    but the groupbox is very high compared to the content.
    In the free space I could place another 5 buttons.
    How I can fit GroupBox to content?
    Thanks.

    Regards.



  • You can set alignment to it:

        hlButtons->addWidget(groupTabs, 0, Qt::AlignLeft | Qt::AlignTop);
    

    void QBoxLayout::addWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment())

    Adds widget to the end of this box layout, with a stretch factor of stretch and alignment alignment.
    The stretch factor applies only in the direction of the QBoxLayout, and is relative to the other boxes and widgets in this QBoxLayout. Widgets and boxes with higher stretch factors grow more.
    If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved.
    The alignment is specified by alignment. The default alignment is 0, which means that the widget fills the entire cell.



  • It work fine :)
    Many thanks.


Log in to reply