Alignment in groupbox without modifying size policy



  • Imagine I have a QVoxLayout with a QGroupBox and a QTreeView. now inside of the QGroupBox I have another QVBoxLayout with 2 buttons that I want aligned to the top but I still want the QTreeView to take any extra space when resizing. Any suggestions? if I use addStrech or addSpacerItem on the inside layout it will compete with the view for extra space



  • @VRonin mmh, in your Top VBoxLayout, set the SizePoilzy of the GroupBox to 'preferred' give it a stretch factor of 0 and your Treeview of 1. void QBoxLayout::setStretch(int index, int stretch)

    Your QGroupBox should resize to the min-height of your Buttons



  • I realised I did not provide enough informations in the question so here is a minimal code that shows the problem:

    #include <QWidget>
    #include <QVBoxLayout>
    #include <QTreeView>
    #include <QGridLayout>
    #include <QGroupBox>
    #include <QLabel>
    #include <QPushButton>
    class TestWidget : public QWidget{
        Q_OBJECT
        Q_DISABLE_COPY(TestWidget)
    public:
        explicit TestWidget(QWidget* parent = Q_NULLPTR)
            : QWidget(parent)
        {
            QGridLayout* mainLay = new QGridLayout(this);
            QTreeView* treeView=new QTreeView(this);
            QGroupBox* groupBox1=new QGroupBox(this);
            QVBoxLayout* group1Lay = new QVBoxLayout(groupBox1);
            QPushButton* button1 = new QPushButton("button1",this);
            QPushButton* button2 = new QPushButton("button1",this);
            QGroupBox* groupBox2=new QGroupBox(this);
            QVBoxLayout* group2Lay = new QVBoxLayout(groupBox2);
            group2Lay->addWidget(new QLabel("SpaceTaker",this));
            group2Lay->addWidget(new QLabel("SpaceTaker",this));
            group2Lay->addWidget(new QLabel("SpaceTaker",this));
            group2Lay->addWidget(new QLabel("SpaceTaker",this));
            group2Lay->addWidget(new QLabel("SpaceTaker",this));
            group1Lay->addWidget(button1);
            group1Lay->addWidget(button2);
            group1Lay->addStretch(); // problem
            mainLay->addWidget(groupBox1,0,0);
            mainLay->addWidget(groupBox2,0,1);
            mainLay->addWidget(treeView,1,0,1,2);
        }
    };
    

    @J-Hilk
    I'll try your solution


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.