QT qmake進階 - 語法

QT qmake進階 - 語法


如需轉載請標明出處:http://blog.csdn.net/itas109
QQ技術交流羣:129518033

相關問題:
1.QT 不同操作系統條件編譯
2.QT在windows下區分msvc和MinGW

環境說明:
QT:5.12.7


1.Operators操作符

1.1 賦值

使用=給變量賦值

TARGET = myapp

1.2 附加值

使用+=給變量附加值

DEFINES += USE_MY_STUFF

1.3 移除值

使用-=移除變量的指定值

DEFINES -= USE_MY_STUFF

1.4 增加唯一值

使用*=爲變量增加唯一的值。該值不會重複。

DEFINES *= USE_MY_STUFF

1.5 替換值

使用~=替換正則表達式的任何值

DEFINES ~= s/QT_[DT].+/QT

上述中的QT_D或QT_T替換爲QT

1.6 變量擴展

使用$$用於提取變量的內容,並可用於在變量之間傳遞值或將其提供給函數

EVERYTHING = $$SOURCES $$HEADERS
message("The project contains the following files:")
message($$EVERYTHING)

運行qmake時使用$$(...)獲取環境值

DESTDIR = $$(PWD)
message(The project will be installed in $$DESTDIR)

1.7 獲取qmake的屬性

使用$$[...]獲取qmake屬性

message(Qt version: $$[QT_VERSION])
message(Qt is installed in $$[QT_INSTALL_PREFIX])
message(Qt resources can be found in the following locations:)
message(Documentation: $$[QT_INSTALL_DOCS])
message(Header files: $$[QT_INSTALL_HEADERS])
message(Libraries: $$[QT_INSTALL_LIBS])
message(Binary files (executables): $$[QT_INSTALL_BINS])
message(Plugins: $$[QT_INSTALL_PLUGINS])
message(Data files: $$[QT_INSTALL_DATA])
message(Translation files: $$[QT_INSTALL_TRANSLATIONS])
message(Settings: $$[QT_INSTALL_CONFIGURATION])
message(Examples: $$[QT_INSTALL_EXAMPLES])

2.Scopes作用域

作用域類似於if語句。如果滿足某個條件,則將處理作用域內的聲明。

2.1 作用域語法

作用域由condition條件組成,後跟同一行的右括號,一系列命令或定義以及新行的右括號

<condition> {
    <command or definition>
    ...
}

2.2 作用域和條件

單個條件

win32 {
    SOURCES += paintwidget_win.cpp
}

條件組合

macx {
    CONFIG(debug, debug|release) {
        HEADERS += debugging.h
    }
}

簡化寫法

macx:CONFIG(debug, debug|release) {
    HEADERS += debugging.h
}

else用法

win32:xml {
    message(Building for Windows)
    SOURCES += xmlhandler_win.cpp
} else:xml {
    SOURCES += xmlhandler.cpp
} else {
    message("Unknown configuration")
}

2.3 配置與作用域

qmake專門處理CONFIG變量中存儲的值。每個可能的值都可以用作作用域的條件。

CONFIG += opengl
opengl {
    TARGET = application-gl
} else {
    TARGET = application
}

2.4 * 平臺作用域

除了win32,macx和unix的作用域條件下使用值,其他各種內置的平臺和編譯器特定的值,可以用於作用域。這些平臺規範由Qt mkspecs目錄提供。例如,linux-g++:

message($$QMAKESPEC)

linux-g++ {
    message(Linux)
}

# msvc *
win32-msvc*:LIBS += Advapi32.lib

# MinGW
win32-g++:LIBS += User32.lib

3.Variables變量

MY_VARIABLE = value

MY_DEFINES = $$DEFINES

MY_DEFINES = $${DEFINES}

TARGET = myproject_$${TEMPLATE}

4.Replace Functions替換函數

qmake提供了一些內置函數,以允許處理變量的內容。

HEADERS = model.h
HEADERS += $$OTHER_HEADERS
HEADERS = $$unique(HEADERS)

可以自定義的函數來處理變量,如下所示:

defineReplace(functionName){
    #function code
}

替換內置函數列表


absolute_path(path[, base])
basename(variablename)
cat(filename[, mode])
clean_path(path)
dirname(file)
enumerate_vars
escape_expand(arg1 [, arg2 ..., argn])
find(variablename, substr)
files(pattern[, recursive=false])
first(variablename)
format_number(number[, options...])
fromfile(filename, variablename)
getenv(variablename)
join(variablename, glue, before, after)
last(variablename)
list(arg1 [, arg2 ..., argn])
lower(arg1 [, arg2 ..., argn])
member(variablename [, start [, end]])
num_add(arg1 [, arg2 ..., argn])
prompt(question [, decorate])
quote(string)
re_escape(string)
read_registry(tree, key[, flag])
relative_path(filePath[, base])
replace(string, old_string, new_string)
resolve_depends(variablename, prefix)
reverse(variablename)
section(variablename, separator, begin, end)
shadowed(path)
shell_path(path)
shell_quote(arg)
size(variablename)
sort_depends(variablename, prefix)
sorted(variablename)
split(variablename, separator)
sprintf(string, arguments...)
str_member(arg [, start [, end]])
str_size(arg)
system(command[, mode[, stsvar]])
system_path(path)
system_quote(arg)
take_first(variablename)
take_last(variablename)
unique(variablename)
upper(arg1 [, arg2 ..., argn])
val_escape(variablename)

5.Test Functions測試函數

qmake提供了內置函數,可以在編寫作用域時用作條件。這些函數沒有返回值,而是指示成功或失敗:

count(options, 2) {
    message(Both release and debug specified.)
}

可以自定義的函數:

defineTest(allFiles) {
    files = $$ARGS

    for(file, files) {
        !exists($$file) {
            return(false)
        }
    }
    return(true)
}

測試內置函數列表


cache(variablename, [set|add|sub] [transient] [super|stash], [source variablename])
CONFIG(config)
contains(variablename, value)
count(variablename, number)
debug(level, message)
defined(name[, type])
equals(variablename, value)
error(string)
eval(string)
exists(filename)
export(variablename)
for(iterate, list)
greaterThan(variablename, value)
if(condition)
include(filename)
infile(filename, var, val)
isActiveConfig
isEmpty(variablename)
isEqual
lessThan(variablename, value)
load(feature)
log(message)
message(string)
mkpath(dirPath)
requires(condition)
system(command)
touch(filename, reference_filename)
unset(variablename)
versionAtLeast(variablename, versionNumber)
versionAtMost(variablename, versionNumber)
warning(string)
write_file(filename, [variablename, [mode]])

6.其他

6.1 qmake命令語法

Usage: qmake [mode] [options] [files]

QMake has two modes, one mode for generating project files based on
some heuristics, and the other for generating makefiles. Normally you
shouldn't need to specify a mode, as makefile generation is the default
mode for qmake, but you may use this to test qmake on an existing project

Mode:
  -project       Put qmake into project file generation mode
                 In this mode qmake interprets [files] as files to
                 be added to the .pro file. By default, all files with
                 known source extensions are added.
                 Note: The created .pro file probably will
                 need to be edited. For example add the QT variable to
                 specify what modules are required.
  -makefile      Put qmake into makefile generation mode (default)
                 In this mode qmake interprets files as project files to
                 be processed, if skipped qmake will try to find a project
                 file in your current working directory

Warnings Options:
  -Wnone         Turn off all warnings; specific ones may be re-enabled by
                 later -W options
  -Wall          Turn on all warnings
  -Wparser       Turn on parser warnings
  -Wlogic        Turn on logic warnings (on by default)
  -Wdeprecated   Turn on deprecation warnings (on by default)

Options:
   * You can place any variable assignment in options and it will be *
   * processed as if it was in [files]. These assignments will be    *
   * processed before [files] by default.                            *
  -o file        Write output to file
  -d             Increase debug level
  -t templ       Overrides TEMPLATE as templ
  -tp prefix     Overrides TEMPLATE so that prefix is prefixed into the value
  -help          This help
  -v             Version information
  -early         All subsequent variable assignments will be
                 parsed right before default_pre.prf
  -before        All subsequent variable assignments will be
                 parsed right before [files] (the default)
  -after         All subsequent variable assignments will be
                 parsed after [files]
  -late          All subsequent variable assignments will be
                 parsed right after default_post.prf
  -norecursive   Don't do a recursive search
  -recursive     Do a recursive search
  -set <prop> <value> Set persistent property
  -unset <prop>  Unset persistent property
  -query <prop>  Query persistent property. Show all if <prop> is empty.
  -qtconf file   Use file instead of looking for qt.conf
  -cache file    Use file as cache           [makefile mode only]
  -spec spec     Use spec as QMAKESPEC       [makefile mode only]
  -nocache       Don't use a cache file      [makefile mode only]
  -nodepend      Don't generate dependencies [makefile mode only]
  -nomoc         Don't generate moc targets  [makefile mode only]
  -nopwd         Don't look for files in pwd [project mode only]

6.2 qmake query命令

D:\Qt\Qt5.12.7\5.12.7\mingw73_32\bin>qmake -query
QT_SYSROOT:
QT_INSTALL_PREFIX:D:/Qt/Qt5.12.7/5.12.7/mingw73_32
QT_INSTALL_ARCHDATA:D:/Qt/Qt5.12.7/5.12.7/mingw73_32
QT_INSTALL_DATA:D:/Qt/Qt5.12.7/5.12.7/mingw73_32
QT_INSTALL_DOCS:D:/Qt/Qt5.12.7/Docs/Qt-5.12.7
QT_INSTALL_HEADERS:D:/Qt/Qt5.12.7/5.12.7/mingw73_32/include
QT_INSTALL_LIBS:D:/Qt/Qt5.12.7/5.12.7/mingw73_32/lib
QT_INSTALL_LIBEXECS:D:/Qt/Qt5.12.7/5.12.7/mingw73_32/bin
QT_INSTALL_BINS:D:/Qt/Qt5.12.7/5.12.7/mingw73_32/bin
QT_INSTALL_TESTS:D:/Qt/Qt5.12.7/5.12.7/mingw73_32/tests
QT_INSTALL_PLUGINS:D:/Qt/Qt5.12.7/5.12.7/mingw73_32/plugins
QT_INSTALL_IMPORTS:D:/Qt/Qt5.12.7/5.12.7/mingw73_32/imports
QT_INSTALL_QML:D:/Qt/Qt5.12.7/5.12.7/mingw73_32/qml
QT_INSTALL_TRANSLATIONS:D:/Qt/Qt5.12.7/5.12.7/mingw73_32/translations
QT_INSTALL_CONFIGURATION:
QT_INSTALL_EXAMPLES:D:/Qt/Qt5.12.7/Examples/Qt-5.12.7
QT_INSTALL_DEMOS:D:/Qt/Qt5.12.7/Examples/Qt-5.12.7
QT_HOST_PREFIX:D:/Qt/Qt5.12.7/5.12.7/mingw73_32
QT_HOST_DATA:D:/Qt/Qt5.12.7/5.12.7/mingw73_32
QT_HOST_BINS:D:/Qt/Qt5.12.7/5.12.7/mingw73_32/bin
QT_HOST_LIBS:D:/Qt/Qt5.12.7/5.12.7/mingw73_32/lib
QMAKE_SPEC:win32-g++
QMAKE_XSPEC:win32-g++
QMAKE_VERSION:3.1
QT_VERSION:5.12.7

License

License under CC BY-NC-ND 4.0: 署名-非商業使用-禁止演繹


Reference:
https://doc.qt.io/archives/qt-5.13/qmake-language.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章