



Qt的程序结构主要包括main()主程序和在主程序中实例化的一系列GUI组件。在Qt自动生成的程序中,主程序实例化应用程序和主窗口MainWindow,并显示主窗口及其包含的控件,然后在主窗口中完成主要的操作和处理,如图1-12所示。
图1-12 Qt程序结构示意图
主程序的程序结构如下:
#include<QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
主窗口所有的控制和操作是通过类的定义来实现的。类的定义如下:
头文件mainwindow.h:
#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
主窗口程序文件mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}