官术网_书友最值得收藏!

  • Qt編程快速入門
  • 鮑忠貴
  • 517字
  • 2021-03-19 17:41:44

1.3 了解Qt生成的工程文件

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++源文件。

主站蜘蛛池模板: 湟源县| 崇左市| 得荣县| 东平县| 永登县| 黔西县| 凤城市| 建始县| 西林县| 开江县| 宜昌市| 广宁县| 会理县| 忻城县| 牙克石市| 香港| 临沭县| 三门峡市| 东海县| 南华县| 麦盖提县| 上杭县| 汕头市| 高台县| 宁陕县| 兴业县| 加查县| 韩城市| 镇坪县| 江门市| 铜陵市| 介休市| 苍南县| 左权县| 甘德县| 界首市| 隆尧县| 共和县| 石门县| 会同县| 成武县|