首页 > 代码库 > QT最简单的程序执行过程分析(内含C++基础知识)

QT最简单的程序执行过程分析(内含C++基础知识)

打开QT Creator,新建一个“应用程序-Qt Widgets Application”项目:

输入名称scdc之后点击下一步。

在“构建套件”这个页面中直接点出下一步,然后再输入自己的类名DataCenter,其它不用手动改过来,QT会自动修改,如下图所示:

再点下一步,完成即可了。

这个时候直接点击左下方的“运行”按键,窗体就会出现了。

先看一看项目列表中所列的文件

这些文件分别是:

  1. datacenter.h
  2. datacenter.cpp
  3. main.cpp
  4. datacenter.ui
  5. ui_datacenter.h 这个文件没有在左边的列表中,而是被datacenter.cpp所包含。

 

datacenter.h

#ifndef DATACENTER_H#define DATACENTER_H#include <QMainWindow>namespace Ui {class DataCenter;}class DataCenter : public QMainWindow{    Q_OBJECTpublic:    explicit DataCenter(QWidget *parent = 0);    ~DataCenter();private:    Ui::DataCenter *ui;};#endif // DATACENTER_H

 

 

datacenter.cpp

#include "datacenter.h"#include "ui_datacenter.h"DataCenter::DataCenter(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::DataCenter){    ui->setupUi(this);}DataCenter::~DataCenter(){    delete ui;}

与其他函数不同,构造函数除了有名字,参数列表和函数体之外,还可以有初始化列表,初始化列表以冒号开头,后跟一系列以逗号分隔的初始化字段。上面的QMainWindow(parent)和ui(new Ui::DataCenter)就是初始化列表。

 

main.cpp

#include "datacenter.h"#include <QApplication>int main(int argc, char *argv[]){    QApplication a(argc, argv);    DataCenter w;    w.show();    return a.exec();}

 

 

ui_datacenter.h

/********************************************************************************** Form generated from reading UI file ‘datacenter.ui‘**** Created by: Qt User Interface Compiler version 5.2.0**** WARNING! All changes made in this file will be lost when recompiling UI file!********************************************************************************/#ifndef UI_DATACENTER_H#define UI_DATACENTER_H#include <QtCore/QVariant>#include <QtWidgets/QAction>#include <QtWidgets/QApplication>#include <QtWidgets/QButtonGroup>#include <QtWidgets/QHeaderView>#include <QtWidgets/QMainWindow>#include <QtWidgets/QMenu>#include <QtWidgets/QMenuBar>#include <QtWidgets/QPushButton>#include <QtWidgets/QStatusBar>#include <QtWidgets/QTextBrowser>#include <QtWidgets/QToolBar>#include <QtWidgets/QWidget>QT_BEGIN_NAMESPACEclass Ui_DataCenter{public:    QAction *actionLicense;    QAction *actionAbout;    QAction *actionNew;    QAction *actionOpen;    QAction *actionSave;    QAction *actionSave_as;    QAction *actionTemp_list;    QAction *actionExit;    QWidget *centralWidget;    QPushButton *pushButton;    QTextBrowser *textBrowser;    QMenuBar *menuBar;    QMenu *File;    QMenu *View;    QMenu *Tool;    QMenu *About;    QToolBar *mainToolBar;    QStatusBar *statusBar;    void setupUi(QMainWindow *DataCenter)    {        if (DataCenter->objectName().isEmpty())            DataCenter->setObjectName(QStringLiteral("DataCenter"));        DataCenter->resize(800, 600);        QIcon icon;        icon.addFile(QStringLiteral(":/res/scdc.ico"), QSize(), QIcon::Normal, QIcon::Off);        DataCenter->setWindowIcon(icon);        actionLicense = new QAction(DataCenter);        actionLicense->setObjectName(QStringLiteral("actionLicense"));        actionLicense->setIcon(icon);        actionAbout = new QAction(DataCenter);        actionAbout->setObjectName(QStringLiteral("actionAbout"));        actionNew = new QAction(DataCenter);        actionNew->setObjectName(QStringLiteral("actionNew"));        actionOpen = new QAction(DataCenter);        actionOpen->setObjectName(QStringLiteral("actionOpen"));        actionSave = new QAction(DataCenter);        actionSave->setObjectName(QStringLiteral("actionSave"));        actionSave_as = new QAction(DataCenter);        actionSave_as->setObjectName(QStringLiteral("actionSave_as"));        actionTemp_list = new QAction(DataCenter);        actionTemp_list->setObjectName(QStringLiteral("actionTemp_list"));        actionExit = new QAction(DataCenter);        actionExit->setObjectName(QStringLiteral("actionExit"));        centralWidget = new QWidget(DataCenter);        centralWidget->setObjectName(QStringLiteral("centralWidget"));        pushButton = new QPushButton(centralWidget);        pushButton->setObjectName(QStringLiteral("pushButton"));        pushButton->setGeometry(QRect(60, 100, 75, 23));        textBrowser = new QTextBrowser(centralWidget);        textBrowser->setObjectName(QStringLiteral("textBrowser"));        textBrowser->setGeometry(QRect(300, 110, 441, 381));        DataCenter->setCentralWidget(centralWidget);        menuBar = new QMenuBar(DataCenter);        menuBar->setObjectName(QStringLiteral("menuBar"));        menuBar->setGeometry(QRect(0, 0, 800, 23));        File = new QMenu(menuBar);        File->setObjectName(QStringLiteral("File"));        View = new QMenu(menuBar);        View->setObjectName(QStringLiteral("View"));        Tool = new QMenu(menuBar);        Tool->setObjectName(QStringLiteral("Tool"));        About = new QMenu(menuBar);        About->setObjectName(QStringLiteral("About"));        DataCenter->setMenuBar(menuBar);        mainToolBar = new QToolBar(DataCenter);        mainToolBar->setObjectName(QStringLiteral("mainToolBar"));        mainToolBar->setMovable(false);        DataCenter->addToolBar(Qt::TopToolBarArea, mainToolBar);        DataCenter->insertToolBarBreak(mainToolBar);        statusBar = new QStatusBar(DataCenter);        statusBar->setObjectName(QStringLiteral("statusBar"));        DataCenter->setStatusBar(statusBar);        menuBar->addAction(File->menuAction());        menuBar->addAction(View->menuAction());        menuBar->addAction(Tool->menuAction());        menuBar->addAction(About->menuAction());        File->addAction(actionNew);        File->addAction(actionOpen);        File->addAction(actionSave);        File->addAction(actionSave_as);        File->addSeparator();        File->addAction(actionTemp_list);        File->addSeparator();        File->addAction(actionExit);        About->addAction(actionLicense);        About->addAction(actionAbout);        retranslateUi(DataCenter);        QMetaObject::connectSlotsByName(DataCenter);    } // setupUi    void retranslateUi(QMainWindow *DataCenter)    {        DataCenter->setWindowTitle(QApplication::translate("DataCenter", "DataCenter - Copyright 2012-2014 senscom", 0));        actionLicense->setText(QApplication::translate("DataCenter", "\350\256\270\345\217\257", 0));        actionAbout->setText(QApplication::translate("DataCenter", "\345\205\263\344\272\216", 0));        actionNew->setText(QApplication::translate("DataCenter", "New", 0));        actionOpen->setText(QApplication::translate("DataCenter", "Open", 0));        actionSave->setText(QApplication::translate("DataCenter", "Save", 0));        actionSave_as->setText(QApplication::translate("DataCenter", "Save as...", 0));        actionTemp_list->setText(QApplication::translate("DataCenter", "temp list....", 0));        actionExit->setText(QApplication::translate("DataCenter", "Exit", 0));        pushButton->setText(QApplication::translate("DataCenter", "PushButton", 0));        File->setTitle(QApplication::translate("DataCenter", "\346\226\207\344\273\266", 0));        View->setTitle(QApplication::translate("DataCenter", "\346\237\245\347\234\213", 0));        Tool->setTitle(QApplication::translate("DataCenter", "\345\267\245\345\205\267", 0));        About->setTitle(QApplication::translate("DataCenter", "\345\205\263\344\272\216", 0));    } // retranslateUi};namespace Ui {    class DataCenter: public Ui_DataCenter {};} // namespace UiQT_END_NAMESPACE#endif // UI_DATACENTER_H

 

QT最简单的程序执行过程分析(内含C++基础知识)