Qt empty space column in QGridLayout?

16,658

Solution 1

Use void QGridLayout::setColumnStretch ( int column, int stretch ).

You can set stretch = 1 for all columns. It will give the view you want.

Solution 2

You can use QGridLayout::setColumnMinumumWidth(int column, int minSize) to set the width of the column column to minSize pixels. This also works for empty columns. In your case, you would do something like this:

insAddPanel->setColumnMinimumWidth(0, 100);    //Makes the empty column 0 100 pixels wide
insAddPanel->setColumnMinimumWidth(1, 100);    //Makes the empty column 1 100 pixels wide
insAddPanel->setColumnMinimumWidth(2, 100);    //Makes the empty column 2 100 pixels wide

Change 100 to the number of pixels that you want the columns to be wide.

Also, if you use this solution, you don't need three columns, one column which is very large will be enough.

There is also a setRowMinimumHeight method that does the same thing for rows.

Solution 3

Use QHBoxLayout, add some spacers, and set correct stretch factors. QSpacerItem - is about your original question.

Are there any reason to use grid layout?

Share:
16,658
nakiya
Author by

nakiya

Feeling lost in the land of gods

Updated on June 28, 2022

Comments

  • nakiya
    nakiya almost 2 years

    I have the following code where I place a button and a text box in a QGridLayout. PLease notice that I use columns 3 and 4 for placing these.

        QGridLayout *insAddPanel = new QGridLayout();
        {
            QLineEdit* ledInstrumentName = new QLineEdit();
            insAddPanel->addWidget(ledInstrumentName, 0, 3);
    
            QPushButton* btnAddInstrument = new QPushButton();
            btnAddInstrument->setText("Add");
            insAddPanel->addWidget(btnAddInstrument, 0, 4);
        }
        mainLayout->addLayout(insAddPanel);
        ......
    

    However when I run this, I get something like this:

    enter image description here

    I wanted the text edit and the button to occupy only 2/5 of the available horizontal space. That is why I placed these in 3rd and 4th column. As this does not work, how do I get this done? Is there something like an empty space widget in Qt? I searched but did not find it.