Alex Rivera | Logout

Qt : what is the difference between layout and widget?

Asked 2010-04-09T16:20:36.200
10

I thought that layout is just a widget that keeps more widgets inside. But now I find that I can't add it to other widgets using addWidget. For instance how to add a layout to QSplitter?

Edit
Report

1 Answer

4

No, a layout is not a widget or a container. A layout is more like a "helper" that's attached to a window and figures out the best place to put each widget.

This example from the Qt docs should help (http://qt.nokia.com/doc/4.2/layout.html):

QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);

window->setLayout(layout);
window->show();
answered 2010-04-09T16:26:40.340

Your Answer