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 = nullptr); 
    ~MainWindow(); 
     
private: 
    Ui::MainWindow *ui; 
}; 
 
#endif // MAINWINDOW_H 

In mainwindow.cpp ...

Get Application Development with Qt Creator now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.