初識Qthtml
1、維基百科定義:linux
Qt (/kjut/ "cute", or unofficially as Q-T cue-tee) is a cross-platform application framework that iswidely used for developing application software with a graphical user interface (GUI) (inwhich cases Qt is classified as awidget toolkit), and also used for developing non-GUI programssuch as command-line tools andconsoles for servers.vim
2、buntu中用命令安裝:數組
sudo apt-get install qt4-dev-tools #開發包 sudo apt-get install qtcreator #IDE sudo apt-get install qt4-doc #開發幫助文檔 sudo apt-get install qt4-qtconfig #配置工具 sudo apt-get install qt4-demos#DEMO源碼
3、在終端下編寫簡單的Qt程序app
查看Qt版本命令:ide
qmake –v Qmake version2.01a Using Qtversion 4.8.1 in /usr/lib/i386-linux-gnu
利用vim創建Qt源文件:函數
mkdir hello cd hello vim hello.cpp
在文件中鍵入下面的代碼:工具
而後咱們用qt的工具qmake來生成工程文件hello.pro:spa
qmake -project
生成Makefile文件:.net
qmake hello.pro
接下來就是make了,等上一段時間.
make
運行程序
./hello
結果:
下面是界面:
分析:
#include <qapplication.h> #include <qpushbutton.h> int main(int argc,char *argv[]) { QApplication app(argc,argv); QpushButton *hello = new QpushButton(「HelloQt! Xpspace!」,0); hello->resize(100,30); hello->show(); return app.exec(); }
註解:
第1、2行,包含兩個類的定義。在每個使用Qt的應用程序中都必須使用一個QApplication對象,QApplication管理了各類各樣的應用程序的普遍資源。QPushButton是一個經典的圖形用戶界面按鈕,用戶能夠按下去,也能夠放開。一個QPushButton能夠顯示一段文本或者一個QPixmap。
第3行,爲main函數是程序的入口。main()只須要在把控制轉交給Qt庫以前執行一些初始化,而後Qt庫經過事件來向程序告知用戶的行爲。argc是命令行變量的數量,argv是命令行變量的數組。
第4行,爲括號,不解釋。
第5行,建立一個QApplication 對象。這個對象用於管理應用程序級別的資源。 QApplication 的構造函數要求兩個參數,分別來自main的那兩個參數,所以,Qt 在必定程度上是支持命令行參數的。
第6行,一個按鈕被建立了。這個按鈕被設置成顯示「Hello Qt! Xpspace!」而且它本身構成了一個窗口。
第7行,這個按鈕被設置成100像素寬,30像素高(加上窗口系統邊框)。
第8行,表示當你建立一個窗口部件的時候,它是不可見的。你必須調用show()來使它變爲可見的。
第9行,表示main()把控制轉交給Qt,而且當應用程序退出的時候exec()就會返回。 在exec()中,Qt接受並處理用戶和系統的事件而且把它們傳遞給適當的窗口部件。
第10行,大括號,程序結束。
註釋:有的程序用到setMainWidget()函數,若是你用的Qt版本是4.0以上的會報錯以下:
error: ‘class QApplication’ has no member named ‘setMainWidget’
錯誤是因爲版本問題,setMainWidget是Qt3的,而在Qt4中,這個函數已經不用了,若是想兼容Qt3,能夠再程序頭文件前加如下語句(不能在頭文件後):
#define QT3_SUPPORT
以後,帶有setMainWidget的程序能夠成功編譯。