Reputation: 1334
I'm building gui for my project using Qt5.5 and trying to use QGridLayout.
This code worked as I expected.
QGridLayout * layout = new QGridLayout;
layout->addWidget(lineEdits[0], 0,0,1,1);
layout->addWidget(lineEdits[1], 0,1,1,1);
layout->addWidget(lineEdits[2], 1,0,1,1);
layout->addWidget(lineEdits[3], 1,1,1,2);
layout->addWidget(lineEdits[4], 2,0,1,2);
layout->addWidget(lineEdits[5], 2,2,1,1);
this->setLayout(layout);
Here is the result I got.
But this code generated broken layout and that was very far from what I expected.
QGridLayout * layout = new QGridLayout;
layout->addWidget(lineEdits[0], 0,0,1,1);
layout->addWidget(lineEdits[1], 0,1,1,2); //this is the only difference
layout->addWidget(lineEdits[2], 1,0,1,1);
layout->addWidget(lineEdits[3], 1,1,1,2);
layout->addWidget(lineEdits[4], 2,0,1,2);
layout->addWidget(lineEdits[5], 2,2,1,1);
this->setLayout(layout);
I have no idea about how to fix this.
I would appreciate for your suggestions in advance.
Upvotes: 1
Views: 442
Reputation: 206679
The problem is that there's nothing that gives the middle column any significant width - it's there but the only thing that makes it visible is the inter-column spacing, which is why the last row doesn't align with the other two.
One way to fix this is to set the column stretch factors. For example, try this (same factor for all columns, the actual number doesn't matter, only the proportion does):
//...
layout->addWidget(lineEdits[5], 2, 2, 1, 1);
for (int i=0; i<3; ++i)
layout->setColumnStretch(i, 1);
this->setLayout(layout);
This gives me the following layout:
Another option is to force one of the multi-column widgets' widths explicitly, but better avoid that if you can.
Upvotes: 2