QT QMake 的使用学习笔记
- 参考文章
- QT程序编译经历的步骤
- 编译Pro文件生成Makefile
- jom或者make编译Makefile
- 生成界面源码
- uic.exe widget.ui -o ui_widget.h
- 生成信号与槽代码
- moc.exe widget.h moc_widget.cpp
- 手写pro 文件,然后执行脚本生成Makefile
- 需要添加qt环境变量(编译的路径)
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" qmake -o makefile testqmake.pro jom /f makefile
- Linux下:
export PATH=/opt/Qt5.9.0/5.9/gcc_64/bin:$PATH qmake -o makefile testqmake.pro make
- qmake的pro工程导入vs
- qmake -tp vc test.pro
- 或者使用插件直接打开
- 显示控制台(app类型)
- CONFIG +=console
- 加载QT内部库
- QT += core gui
- QT -= gui
- 头文件引用
- INCLUDEPATH += $$PWD/../../include
- PWD 是.pro所在的文件路径
- 可以在.pro文件中使用 message($$PWD)查看
- 库引用和库路径指定
- 《QT中的LIBS用法》
- LIBS += -L../../lib -lopencv_world320
- linux libopencv_world320.so
- windows opencv_world320.lib
- 指定编译输出文件,工作路径(可放置程序运行依赖库)
- DESTDIR += ../../bin
- 指定生成程序的名字:
- TARGET = Demo
- QT创建动态库
- TEMPLATE = lib
- 静态库还需添加: CONFIG += staticlib
- 通过QT 工具创建
- 选择类型:
- 手动创建库
- .pro文件
SOURCES +=xsocket.cpp HEADERS += xsocket.h \ xglobal.h TEMPLATE=lib DEFINES += LIBDLL_LIB DESTDIR = "../../lib" DLLDESTDIR = "../../bin" #CONFIG +=staticlib CONFIG(debug,debug|release){ TARGET = libdll_d }else{ TARGET=libdll } message($$TARGET)
- 跨平台头文件定义
#ifndef XGLOBAL_H #define XGLOBAL_H #include <QtCore/qglobal.h> #ifdef LIBDLL_LIB #define XEXPORT Q_DECL_EXPORT #else #define XEXPORT Q_DECL_IMPORT #endif #endif // XGLOBAL_H
- 跨平台编译项目
- 预设平台:win32 ,linux, unix, macx
- 可通过 message($$QMAKESPEC)查看
win32:TARGET = winmkspecs linux:TARGET = linuxmkspecs win32{ message(win32) } linux{ message(linux) } !linux{ message("not linux") } !win32{ message("not windows") } win32|linux{ message("win or linux") } message($$QMAKESPEC) win32-g++{ message(win32-g++) } win32-msvc{ message(win32-msvc) } SOURCES += \ main.cpp \ widget.cpp HEADERS += \ widget.h FORMS += \ widget.ui