Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt empty space column in QGridLayout?

Tags:

c++

layout

qt

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.

like image 544
nakiya Avatar asked Feb 17 '14 09:02

nakiya


3 Answers

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

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

like image 177
Ashot Avatar answered Nov 14 '22 06:11

Ashot


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.

like image 29
Donald Duck Avatar answered Nov 14 '22 07:11

Donald Duck


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

Are there any reason to use grid layout?

like image 1
Dmitry Sazonov Avatar answered Nov 14 '22 06:11

Dmitry Sazonov