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:
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.
Use void QGridLayout::setColumnStretch ( int column, int stretch ).
You can set stretch = 1
for all columns. It will give the view you want.
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.
Use QHBoxLayout, add some spacers, and set correct stretch factors. QSpacerItem - is about your original question.
Are there any reason to use grid layout?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With