Qt First Development Steps

Objectives

  • Learn how to write a simple Qt application
  • Understand the need for layout management
  • Be able to find answers to questions about Qt

How to Write a Simple Qt Application

  1. Download Qt
  2. Install Qt
  3. qt-hello-world

Layout Managers

Introduction

  • The Qt layout system provides a simple way to automatically arrange widgets.
  • These layouts automatically position and resize widgets when the amount of space available for them changes.
  • Qt provides several classes that lay out widgets on a form: QHBoxLayout, QVBoxLayout, QGridLayout, and QStackLayout.

Examples

Example 1: Demonstration of the three basic layout managers. Comment other layouts to see the difference.

#include <QtGui>
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
 
    QDialog *myDialog = new QDialog();
 
    QLabel *labelUsername = new QLabel("Username");
    QLabel *labelPassword = new QLabel("Password");
    QLineEdit *editUsername = new QLineEdit();
    QLineEdit *editPassword = new QLineEdit();
 
    QVBoxLayout *mainLayout = new QVBoxLayout; //vertically
    //QHBoxLayout *mainLayout = new QHBoxLayout(); //horizontally
    //QGridLayout *mainLayout = new QGridLayout(); //grid
    mainLayout->addWidget(labelUsername);
    mainLayout->addWidget(editUsername);
    mainLayout->addWidget(labelPassword);
    mainLayout->addWidget(editPassword);
    myDialog->setLayout(mainLayout);
 
    return myDialog->exec();
}

Example 2: The QStackedLayout class lays out a set of child widgets and shows only one at a time, hiding the others from the user.

#include <QtGui>
#include <QApplication>
 
class Widget : public QWidget
{
public:
    Widget(QWidget *parent = 0);
};
 
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
 
    QLabel *labelUsername = new QLabel("Username");
    QLineEdit *editUsername = new QLineEdit();
    QWidget *firstPageWidget = new QWidget;
    QVBoxLayout *firstLayout = new QVBoxLayout();
    firstLayout->addWidget(labelUsername);
    firstLayout->addWidget(editUsername);
    firstPageWidget->setLayout(firstLayout);
 
    QWidget *secondPageWidget = new QWidget;
    QLabel *labelPassword = new QLabel("Password");
    QLineEdit *editPassword = new QLineEdit();
    QVBoxLayout *secondLayout = new QVBoxLayout();
    secondLayout->addWidget(labelPassword);
    secondLayout->addWidget(editPassword);
    secondPageWidget->setLayout(secondLayout);
 
    QStackedLayout *stackedLayout = new QStackedLayout;
    stackedLayout->addWidget(firstPageWidget);
    stackedLayout->addWidget(secondPageWidget);
 
    QComboBox *pageComboBox = new QComboBox;
    pageComboBox->addItem(tr("Set Username"));
    pageComboBox->addItem(tr("Set Password"));
    connect(pageComboBox, SIGNAL(activated(int)),
            stackedLayout, SLOT(setCurrentIndex(int)));
 
    QVBoxLayout *mainLayout = new QVBoxLayout;
 
    mainLayout->addWidget(pageComboBox);
 
    mainLayout->addLayout(stackedLayout);
    setLayout(mainLayout);
 
}
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Widget widget;
    widget.show();
    return app.exec();
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.