



Qt生成的工程文件包括工程文件、样式文件、头文件和源文件,在Qt Creator集成环境编辑模式中可以分类看到,双击相应的文件,就会在编辑窗口中打开,如图1-11所示。这些文件存放在QT\Ch1目录中。
图1-11 编辑窗口快捷面板
1.工程文件:*.pro
工程文件定义了Qt用的类库、目标程序的文件名,采用模板、C++源文件、C++头文件和样式文件,如下所示:
#-------------------------------------------------
#
#Project created by QtCreator 2013-11-27T15:34:33
#
#-------------------------------------------------
QT +=core gui
TARGET=Ch1
TEMPLATE=app
SOURCES+=main.cpp\
mainwindow.cpp
HEADERS +=mainwindow.h
FORMS +=mainwindow.ui
2.样式文件: *.ui
样式文件定义了人机交互界面,在设计模式下是所见即所得的窗口和控件,在编辑模式下是XML定义文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget"/>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>17</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
Qt编译器在编译样式文件时,自动将XML资源文件编译成C++头文件ui_mainwindow.h。该文件创建了主窗口界面类Ui_MainWindow,封装了样式中的窗口和控件。该文件在QT\Ch1-build-desktop目录中。了解该文件可以更好地理解Qt界面的实现过程。
/********************************************************************************
** Form generated from reading UI file 'mainwindow.ui'
**
** Created: Wed Nov 27 16:20:21 2013
** by: Qt User Interface Compiler version 4.7.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
#include<QtCore/QVariant>
#include<QtGui/QAction>
#include<QtGui/QApplication>
#include<QtGui/QButtonGroup>
#include<QtGui/QHeaderView>
#include<QtGui/QMainWindow>
#include<QtGui/QMenuBar>
#include<QtGui/QStatusBar>
#include<QtGui/QToolBar>
#include<QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public:
QMenuBar *menuBar;
QToolBar *mainToolBar;
QWidget *centralWidget;
QStatusBar *statusBar;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(400, 300);
menuBar=new QMenuBar(MainWindow);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
MainWindow->setMenuBar(menuBar);
mainToolBar=new QToolBar(MainWindow);
mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
MainWindow->addToolBar(mainToolBar);
centralWidget=new QWidget(MainWindow);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
MainWindow->setCentralWidget(centralWidget);
statusBar=new QStatusBar(MainWindow);
statusBar->setObjectName(QString::fromUtf8("statusBar"));
MainWindow->setStatusBar(statusBar);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow",
"MainWindow", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MAINWINDOW_H
3. 头文件: *.h
头文件列出本工程包含的用户头文件。
4. 源文件: *.cpp
源文件列出本工程包含的用户C++源文件。