Book Image

Application Development with Qt Creator - Second Edition

Book Image

Application Development with Qt Creator - Second Edition

Overview of this book

Table of Contents (20 chapters)
Application Development with Qt Creator Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Instantiating forms, message boxes, and dialogs in your application


Qt Designer generates an XML-based layout file (which ends in .ui) for each form you create in Designer. At compile time, Qt Creator compiles the layout into a header file that constructs the components for your user interface layout. The pattern typically used by Qt applications is to construct a private layout class that the main class instantiates. Here's how it works for the main window:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
  class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

In mainwindow.cpp, we have:

#include "mainwindow.h"
#include "resultdialog.h"
#include "ui_mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
{
 ...