Qt pro轉CMake工程例子

2021-10-05 18:18:39 字數 3126 閱讀 6345

去掉一些注釋後

qt       += core gui

greaterthan(qt_major_version, 4): qt += widgets

target = qtdemo

defines += qt_deprecated_warnings

sources += \

main.cpp \

widget.cpp

headers += \

widget.h

類似pro檔案,也增加qt, target, sources, headers幾個變數,只考慮qt5

cmake_minimum_required(version 3.13)

project(qtdemo)

set(target qtdemo)

set(cmake_cxx_standard 17)

set(cmake_automoc on)

set(sources main.cpp widget.cpp)

set(headers widget.h)

set(qt core gui widgets)

find_package(qt5 required $

)add_executable($$

$)qt5_use_modules($$

)

注意到qt5_use_modules這個巨集,它可以讓我們很方便的使用qt的模組

但是this macro is obsolete. use target_link_libraries with imported targets instead.

(這個巨集已經棄用,推薦使用基於target的target_link_libraries)

所以,現在改為基於target的寫法

cmake_minimum_required(version 3.13)

project(qtdemo)

set(target qtdemo)

set(cmake_cxx_standard 17)

set(cmake_automoc on)

set(sources main.cpp widget.cpp)

set(headers widget.h)

set(qt core gui widgets)

find_package(qt5 required $

)add_executable($$

$)target_link_libraries(

$ qt5::core

qt5::gui

qt5::widgets

)

qt這個變數可以設定為哪些值?

以deepin 15.9為例,可以看/usr/lib/x86_64-linux-gnu/cmake這個目錄下有哪些以qt5開頭的資料夾,就可以填去掉qt5字首後的值

(首先使用find_package指令找到qt標頭檔案和庫檔案的位置,然後用target_link_libraries指令使用qt的標頭檔案和庫檔案)

參考:qt5 cmake manual

qt pro配置

qt       += core gui

greaterthan(qt_major_version, 4): qt += widgets

target = qtdemo

defines += qt_deprecated_warnings

sources += \

main.cpp \

widget.cpp

headers += \

widget.h

forms += \

widget.ui

resources += \

qrc.qrc

cmake配置加了ui檔案和qrc資源檔案,要增加對它們的處理

cmake_minimum_required(version 3.13)

project(qtdemo)

set(target qtdemo)

set(cmake_cxx_standard 17)

set(cmake_automoc on)

set(cmake_autorcc on)

set(cmake_autouic on)

set(sources main.cpp widget.cpp)

set(headers widget.h)

set(forms widget.ui)

set(resources qrc.qrc)

set(qt core gui widgets)

find_package(qt5 required $

)add_executable($$

$$$)

target_link_libraries(

$ qt5::core

qt5::gui

qt5::widgets

)

考慮翻譯檔案在我目前找到的資料中,翻譯檔案的處理一般是這樣的

• 翻譯檔案(ts)放在單獨的translations資料夾下

• 除錯時不生成翻譯二進位制檔案(qm),release時通過shell指令碼生成qm檔案

shell指令碼

#!/bin/bash

# this file is used to auto-generate .qm file from .ts file.

# author: shibowen at linuxdeepin.com

ts_list=(`

ls translations/*.ts`

)for ts in

"$"do

printf

"\nprocess $\n"

lrelease "$"

done

Qt pro工程轉sln工程

如題 qmake命令生成vs的vcproj檔案。1 預設情況下,即環境變數qmakespec為你裝的qt for vs的版本,預設生成的為該版本的vs工程檔案。例如,你裝的是qt for vs2010,環境變數qmakespec win32 msvc2010,預設情況下生成的是vc2010的工程檔案...

CMake 例子五六

例子五 前面還是有一點不爽 如果想讓可執行檔案在 bin 目錄,庫檔案在 lib 目錄怎麼辦?就像下面顯示的一樣 build bin hello.exe lib hello.lib 一種辦法 修改頂級的 cmakelist.txt 檔案 project hello add subdirectory ...

Cmake構建OpenCV測試例子

適用cmake的cmakelists.txt來編譯乙個opencv的測試例子。cmakelists.txt的寫法為 project main 新增標頭檔案路徑 include directories usr local opencv347 include usr local opencv347 in...