Qt文档阅读笔记-qmake入门指南
目錄
?
qmake的作用
簡單的小例子
跨平臺的例子
?
qmake的作用
1.生成Makefile文件;
2.生成moc和uic文件;
3.用vs打開Qt時無需要改變pro文件就能能夠生成項目;
在Linux中qmake的主要目的是簡化程序員編寫Makefile;
?
簡單的小例子
手寫一個簡單的pro文件,用再用qmake下;
首先寫一個小例子:
包含4個文件:
hello.h、hello.cpp、main.cpp、hello.pro
源碼如下:
hello.h
#include <iostream> using namespace std;class Hello{ public:Hello();void print(); };hello.cpp
#include "hello.h"Hello::Hello(){cout << "Hello construction called!" << endl; } void Hello::print(){cout << "Hello::print called!" << endl; }main.cpp
#include "hello.h"int main(){Hello *hello = new Hello;hello->print();delete hello;return 0; }hello.pro
TARGET = helloworldCONFIG += debugHEADERS += hello.hSOURCES += hello.cpp \main.cpp?
TARGET一般與pro文件同名,但后綴名應平臺而異,如exe是Windows平臺上的,無后綴是Unix上的,如果想設置一個其他的名字,可以在此設置其他名稱;
使用這條命令生成Makefile
qmake -o Makefile hello.pro
Linux的操作如下圖所示:
隨后使用make即可
在VS中想上次vc的文件要使用下面這條命令:
qmake -tp vc hello.pro?-spec win32-msvc2012
Windows的操作如下圖:
設置好,vs,如果沒輸出,注意設置控制臺輸入,如下圖所示:
運行截圖如下:
?
跨平臺的例子
存在下面幾個文件:
hello.cpp、hello.h、hellounix.cpp、hellounix.h、hellowin.cpp、hellowin.h、main.cpp、hello.pro
具體內容如下:
hello.h
#include <iostream> using namespace std;class Hello{ public:Hello();void print(); };hello.cpp
#include "hello.h"Hello::Hello(){cout << "Hello construction called!" << endl; } void Hello::print(){cout << "Hello::print called!" << endl; }hellounix.h
#include <iostream> using namespace std;class UnixHello{ public:UnixHello();void print(); };hellounix.cpp
#include "hellounix.h"UnixHello::UnixHello(){cout << "UnixHello construction called!" << endl; }void UnixHello::print(){cout << "UnixHello construction called!" << endl; }hellowin.h
#include <iostream> using namespace std;class WinHello{ public:WinHello();void print(); };hellowin.cpp
#include "hellowin.h"WinHello::WinHello(){cout << "WinHello contraction is called!" << endl; }void WinHello::print(){cout << "WinHello print() called!" << endl; }main.cpp
#include "hello.h" #include <QApplication>#ifdef Q_OS_WIN32 #include "hellowin.h" #else #include "hellounix.h" #endifint main(){Hello *hello = new Hello;hello->print();delete hello;#ifdef Q_OS_WIN32WinHello *winHello = new WinHello;winHello->print(); #elseUnixHello *unixHello = new UnixHello;unixHello->print(); #endifreturn 0; }hello.pro
TARGET = helloworldCONFIG += debugHEADERS += hello.hSOURCES += hello.cpp \main.cppwin32 {HEADERS += hellowin.hSOURCES += hellowin.cpp } unix {HEADERS += hellounix.hSOURCES += hellounix.cpp }在Linux上運行截圖如下:
在Windows上運行截圖如下:
?
如果想調試程序可以使用CONFIG這個變量,標記程序為debug版本
如下所示:
源碼如下:
CONFIG += debugHEADERS += hello.hSOURCES += hello.cppSOURCES += main.cpp如果想調試程序可以使用CONFIG這個變量,標記程序為debug版本
如下所示:
使用win32與unix來決定跨平臺的特性【前提是各個平臺的代碼分離】
如下所示:
exists命令,當缺少文件的時候會停止qmake
win32 {debug {CONFIG += console}}后面這個是在Windows上,想調出控制臺的時候(qDebug能打印),需要做的事情
總結
以上是生活随笔為你收集整理的Qt文档阅读笔记-qmake入门指南的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt工作笔记-QCustomplot绘制
- 下一篇: Qt工作笔记-获取选中的文件名(last