Qt快速深入入門(或Qt工程檔案詳解)
學習和使用Qt有一段時間了,做一些簡單的回顧吧。
Qt是諾基亞公司的一款開源GUI軟體架構,本身是基於C++的。運行效率非常的高,不僅可以用於手機應用程式開發,同時可以用來開放傳統型應用程式。整合了眾多GUI類如QMainWindow,QWidget, QDialog等,同時也重新定義了多個STL類,如QList,QVector, QHash, QMap等。使用起來非常的方便,直接在Qt架構下基本不許要其他的外用庫。下面開始吧。
首先安裝Qt整合式開發環境,可以直接上官網上下載。內部整合IDE: Qt Creator和Qt的庫檔案以及協助文檔。
建立一個工程test,選擇Qt Gui application。
建立的工程下一般包含test.pro,mainwindow.h, mainwindow.cpp, mainwindow.ui, main.cpp。下面一個個解釋裡面的內容。
第一部分:test.pro檔案
QT += core gui
TARGET = test
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
其中
QT += core gui 表明使用的是使用Qt的Core和Gui模組。QT將自己的庫函數分為多個模組,最常用的是QtCore, QtGui。如果使用其他的模組的話可以添加其他模組。常用的有QtNetwork,QtOpenGl, QtSql, QtXml. QtWebkit等等,如果應用程式使用到該模組,需要添加相應的模組。
TARGET當然是產生的應用程式或者連結庫的名字。
TEMPLATE是工程的類型,一般有app和lib,app就是直接的應用程式,lib就是動態連結程式庫,一般用於外掛程式開發。
SOURCES就是*.cpp檔案清單,多行顯示的時候用\
HEADERS是*.h檔案清單
FORMS是UI檔案的列表,UI檔案是Qt特有的介面設計檔案(後面介紹)。
如果不想用IDE直接產生工程,完全可以自己寫一個*.pro。
第二部分: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
該檔案是類的聲明檔案。符合主流的OOP的面向介面編程的思想。一般將類的定義都在此檔案中。一般不再該檔案中定義操作。注意繼承自Qt的內部檔案,一定在類中加上Q_OBJECT的宏定義,Qt會使用MOC將改檔案重新編譯成moc_mainwindow.cpp檔案,這個才是原始的C++類的檔案。所以Qt內建的類檔案都不是原生態的C++類,需要Qt編譯器再處理,所以繼承子Qt的類一定要添加該宏定義,否則編譯一定不通過。
Ui::MainWindow* ui變數聲明是mainwindow.ui產生的布局類Ui::MainWindow.
第三部分: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;
}
是類的定義檔案。注意#include “ui_mainwindow.h". 工程中的mainwindow.ui檔案會被UIC編譯產生ui_mainwindow.h檔案中的類就是Ui::MainWindow。MainWindow中的ui就是用布局類初始化。之後在建構函式需要使用ui->setupUI(this);就可以將Ui::MainWindow中的布局應用到本地的MainWindow。
第四部分:mainwindow.ui就是布局檔案
<?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>27</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>
發現其中的內容是xml格式的標記檔案,這點和主流的UI設計相符,現在主流的UI設計如Andriod和WP都是使用XML檔案做UI設計,可以使用Qt SDK內建的Qt Designer進行可視化設計,設計好的*.ui檔案可以直接在工程檔案中使用。主要.pro中FORMS中添加對應的檔案名稱,*.cpp檔案中加入#include "ui_*.h"檔案名稱。初始化之後就可以將布局建立在自己的類中。
轉載地址:http://blog.sina.com.cn/s/blog_ac823e7e010140od.html
在原部落格的基礎上進行了簡單修訂。