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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itas109

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值