Tuesday, July 6, 2010

QVBoxLayout and QHBoxLayout in Qt

Here is a example of using QVBoxLayout and QHBoxLayout.

QVBoxLayout and QHBoxLayout in Qt

main.cpp

#include <QtGui>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);

QWidget* mainwindow = new QWidget();
QVBoxLayout* mainlayout = new QVBoxLayout();

QLabel* label1 = new QLabel("label 1", mainwindow);
QTextEdit* textedit1 = new QTextEdit(mainwindow);
QPushButton* pushbutton1 = new QPushButton("PushButton 1", mainwindow);
QVBoxLayout* layout1 = new QVBoxLayout();
layout1->addWidget(label1);
layout1->addWidget(textedit1);
layout1->addWidget(pushbutton1);

QLabel* label2 = new QLabel("label 2", mainwindow);
QTextEdit* textedit2 = new QTextEdit(mainwindow);
QPushButton* pushbutton2 = new QPushButton("PushButton 2", mainwindow);
QHBoxLayout* layout2 = new QHBoxLayout();
layout2->addWidget(label2);
layout2->addWidget(textedit2);
layout2->addWidget(pushbutton2);


mainwindow->setLayout(mainlayout);
mainlayout->addLayout(layout1);
mainlayout->addLayout(layout2);

mainwindow->show();
return app.exec();
}