PDA

View Full Version : QGridLayout woes



para
16th October 2006, 18:37
I'm not exactly new to Qt, I've written 2-3 decent programs that use it before, but this is the first time I've used Qt4 and I'm having some basic layout problems.

I'm trying to create a single window with a 2x2 grid, where the top row is merged together and has a single push button titles "Top". The lower row should have one push button per cell, "Bottom-Left" and "Bottom-Right".

The problem is the top button does not appear to be expanding along both columns as it should. It also seems the QGridLayout is ignoring the second row -- while that row is being rendered, it overlaps the first and does not appear to be properly spaced.

Has the method of doing this changed since Qt3?

Here's the code:

#include <qapplication.h>

#include <qwidget.h>
#include <qpushbutton.h>
#include <qgridlayout.h>

class gui : public QWidget {
Q_OBJECT

public:
gui();

private:
QPushButton* pb;
QPushButton* pb1;
QPushButton* pb2;

QGridLayout* base;
};

gui::gui() {
base = new QGridLayout(this);

pb = new QPushButton("Top");
pb1 = new QPushButton("Bottom-Left");
pb2 = new QPushButton("Bottom-Right");

base->addWidget(pb1, 1, 0);
base->addWidget(pb2, 1, 1);
base->addWidget(pb, 0, 0, 0, 1);
}

int main(int argc, char** argv) {

QApplication app(argc, argv);



gui* w = new gui();



w->show();



app.exec();



delete w;

return 0;

}

Thanks.

jacek
16th October 2006, 19:18
base->addWidget(pb, 0, 0, 0, 1);
It works with "base->addWidget(pb, 0, 0, 1, 0);". It looks like rowSpan and columnSpan parameters are switched.

para
16th October 2006, 21:49
I can't believe I didn't notice that! Thanks a lot.
I seem to remember Qt3's parameters being (fromRow, toRow, FromCol, toCol) -- really had me stumped.

jacek
16th October 2006, 22:41
I seem to remember Qt3's parameters being (fromRow, toRow, FromCol, toCol)
In Qt4 they are (fromRow, fromCol, rowSpan, colSpan), so actually it should be:

base->addWidget(pb, 0, 0, 1, 2)