PDA

View Full Version : Widgets in QGridLayout placed at top left



anarion
2nd September 2010, 14:16
Hi, It's my first time trying Qt and followed the tutorials on widgets first, then the address book tutorial. My problem is with arranging widgets in a QGridLayout, I use proper row/column numbers but when I run the program, all of them are palced at top left.

/Here's the code\
adressbook.hpp:

#include <QWidget>
#include <QLineEdit>
#include <QTextEdit>

class address_book: public QWidget {
Q_OBJECT
QLineEdit name;
QTextEdit address;

public:
address_book(QWidget *parent=0);
};

addressbook.cpp:

#include "addressbook.hpp"
#include <QLabel>
#include <QGridLayout>

address_book::address_book(QWidget *parent): QWidget(parent), name(), address() {
QLabel lname(tr("Name:"));
QLabel laddress(tr("Address:"));

QGridLayout layout(this);
layout.setSpacing(10);

layout.addWidget(&lname, 0, 0);
layout.addWidget(&name, 0, 1);

layout.addWidget(&laddress, 1, 0);
layout.addWidget(&address, 1, 1);

setWindowTitle(tr("Address Book"));
}
Here's the screenshot:
5132

What might be causing this behaviour?

Lykurg
2nd September 2010, 14:57
Create your layout on the heap, not on the stack! Right now it will be deleted after the constructor, so that in the end the widgets are not layouted (since their layout is getting deleted...)

anarion
2nd September 2010, 15:06
damn... wasn't looking at it from that aspect :D
Thanks! it works.