qmake provides functions for processing the contents of variables during the configuration process. These functions are called
replace functions
. Typically, they return values that you can assign to other variables. You can obtain these values by prefixing a function with the
$$
operator. Replace functions can be divided into built-in functions and function libraries.
另请参阅 测试函数 .
基本替换函数被实现成内置函数。
返回绝对路径的
path
.
若
base
is not specified, uses the current directory as the base directory. If it is a relative path, it is resolved relative to the current directory before use.
例如,以下调用返回字符串
"/home/johndoe/myproject/readme.txt"
:
message($$absolute_path("readme.txt", "/home/johndoe/myproject"))
该函数在 Qt 5.0 引入。
另请参阅 clean_path() , relative_path() .
Returns the basename of the file specified in
variablename
.
例如:
FILE = /etc/passwd FILENAME = $$basename(FILE) #passwd
Returns the contents of
filename
. You can specify the following options for
mode
:
blob
returns the entire contents of the file as one value
lines
returns each line as a separate value (without line endings)
true
(default value) and
false
return file contents as separate values, split according to qmake value list splitting rules (as in variable assignments). If
mode
is
false
, values that contain only a newline character are inserted into the list to indicate where line breaks were in the file.
返回
path
with directory separators normalized (converted to "/") and redundant ones removed, and "."s and ".."s resolved (as far as possible). This function is a wrapper around
QDir::cleanPath
.
该函数在 Qt 5.0 引入。
另请参阅 absolute_path() , relative_path() , shell_path() , system_path() .
Returns the directory name part of the specified file. For example:
FILE = /etc/X11R6/XF86Config DIRNAME = $$dirname(FILE) #/etc/X11R6
Returns a list of all defined variable names.
该函数在 Qt 5.0 引入。
Accepts an arbitrary number of arguments. It expands the escape sequences
\n
,
\r
,
\t
for each argument and returns the arguments as a list.
注意: If you specify the string to expand literally, you need to escape the backslashes, as illustrated by the following code snippet:
message("First line$$escape_expand(\\n)Second line")
Returns all the values in
variablename
that match the regular expression
substr
.
MY_VAR = one two three four MY_VAR2 = $$join(MY_VAR, " -L", -L) -Lfive MY_VAR3 = $$member(MY_VAR, 2) $$find(MY_VAR, t.*)
MY_VAR2 will contain '-Lone -Ltwo -Lthree -Lfour -Lfive', and MY_VAR3 will contain 'three two three'.
Expands the specified wildcard pattern and returns a list of filenames. If
recursive
is true, this function descends into subdirectories.
Returns the first value of
variablename
.
例如,以下调用返回
firstname
:
CONTACT = firstname middlename surname phone message($$first(CONTACT))
另请参阅 take_first (), last() .
返回
编号
in the format specified by
options
. You can specify the following options:
ibase=n
sets the base of the input to
n
obase=n
sets the base of the output to
n
width=n
sets the minimum width of the output to
n
. If the output is shorter than
width
, it is padded with spaces
zeropad
pads the output with zeroes instead of spaces
padsign
prepends a space to positive values in the output
alwayssign
prepends a plus sign to positive values in the output
leftalign
places the padding to the right of the value in the output
Floating-point numbers are currently not supported.
For example, the following call converts the hexadecimal number
BAD
to
002989
:
message($$format_number(BAD, ibase=16 width=6 zeropad))
该函数在 Qt 5.0 引入。
Evaluates
filename
as a qmake project file and returns the value assigned to
variablename
.
另请参阅 infile() .
Returns the value of the environment variable
variablename
. This is mostly equivalent to the
$$(variablename)
syntax. The
getenv
function, however, supports environment variables with parentheses in their name.
该函数在 Qt 5.12 引入。
Joins the value of
variablename
with
glue
. If this value is not empty, this function prefixes the value with
before
and suffixes it with
after
.
variablename
is the only required field, the others default to empty strings. If you need to encode spaces in
glue
,
before
,或
after
, you must quote them.
Returns the last value of
variablename
.
例如,以下调用返回
phone
:
CONTACT = firstname middlename surname phone message($$last(CONTACT))
Takes an arbitrary number of arguments. It creates a uniquely named variable that contains a list of the arguments, and returns the name of that variable. You can use the variable to write a loop as illustrated by the following code snippet
for(var, $$list(foo bar baz)) { ... }
instead of:
values = foo bar baz for(var, values) { ... }
Takes an arbitrary number of arguments and converts them to lower case.
另请参阅 upper() .
Returns the slice of the list value of
variablename
with the zero-based element indices between
start
and
end
(inclusive).
若
start
is not given, it defaults to zero. This usage is equivalent to
$$first(variablename)
.
若
end
不给定,默认为
start
. This usage represents simple array indexing, as exactly one element will be returned.
It is also possible to specify start and end in a single argument, with the numbers separated by two periods.
Negative numbers represent indices starting from the end of the list, with -1 being the last element.
If either index is out of range, an empty list is returned.
若
end
小于
start
, the elements are returned in reverse order.
注意: The fact that the end index is inclusive and unordered implies that an empty list will be returned only when an index is invalid (which is implied by the input variable being empty).
另请参阅 str_member ().
Takes an arbitrary number of numeric arguments and adds them up, returning the sum.
Subtraction is implicitly supported due to the possibility to simply prepend a minus sign to a numeric value to negate it:
sum = $$num_add($$first, -$$second)
If the operand may be already negative, another step is necessary to normalize the number:
second_neg = -$$second second_neg ~= s/^--// sum = $$num_add($$first, $$second_neg)
该函数在 Qt 5.8 引入。
Displays the specified
question
, and returns a value read from stdin.
若
decorate
is
true
(the default), the question gets a generic prefix and suffix identifying it as a prompt.
Converts a whole
string
into a single entity and returns the result. This is just a fancy way of enclosing the string into double quotes.
返回
string
with every special regular expression character escaped with a backslash. This function is a wrapper around
QRegularExpression::escape
.
Returns the value of registry key
key
inside the tree
tree
.
Only the trees
HKEY_CURRENT_USER
(
HKCU
) 和
HKEY_LOCAL_MACHINE
(
HKLM
) are supported.
flag
可以是
WOW64_32KEY
(
32
) 或
WOW64_64KEY
(
64
).
注意: This function is available only on Windows hosts.
This function was introduced in Qt 5.12.1.
Returns the path to
filePath
relative to
base
.
若
base
is not specified, it is the current project directory. If it is relative, it is resolved relative to the current project directory before use.
若
filePath
is relative, it is first resolved against the base directory; in that case, this function effectively acts as $$clean_path().
该函数在 Qt 5.0 引入。
另请参阅 absolute_path() , clean_path() .
Replaces each instance of
old_string
with
new_string
in the contents of the variable supplied as
string
. For example, the code
MESSAGE = This is a tent. message($$replace(MESSAGE, tent, test))
prints the message:
This is a test.
This is an internal function that you will typically not need.
该函数在 Qt 5.0 引入。
Returns the values of
variablename
in reverse order.
该函数在 Qt 5.0 引入。
Returns a section of the value of
variablename
. This function is a wrapper around
QString::section
.
For example, the following call outputs
surname
:
CONTACT = firstname:middlename:surname:phone message($$section(CONTACT, :, 2, 2))
Maps the path from the project source directory to the build directory. This function returns
path
for in-source builds. It returns an empty string if
path
points outside of the source tree.
该函数在 Qt 5.0 引入。
Converts all directory separators within
path
to separators that are compatible with the shell that is used while building the project (that is, the shell that is invoked by the make tool). For example, slashes are converted to backslashes when the Windows shell is used.
该函数在 Qt 5.0 引入。
另请参阅 system_path() .
Quotes
arg
for the shell that is used while building the project.
该函数在 Qt 5.0 引入。
另请参阅 system_quote() .
Returns the number of values of
variablename
.
另请参阅 str_size ().
This is an internal function that you will typically not need.
该函数在 Qt 5.0 引入。
Returns the list of values in
variablename
with entries sorted in ascending ASCII order.
Numerical sorting can be accomplished by zero-padding the values to a fixed length with the help of the format_number () 函数。
该函数在 Qt 5.8 引入。
Splits the value of
variablename
into separate values, and returns them as a list. This function is a wrapper around
QString::split
.
例如:
CONTACT = firstname:middlename:surname:phone message($$split(CONTACT, :))
Replaces %1-%9 in
string
with the arguments passed in the comma-separated list of function
arguments
and returns the processed string.
This function is identical to member (), except that it operates on a string value instead of a list variable, and consequently the indices refer to character positions.
This function can be used to implement many common string slicing operations:
# $$left(VAR, len) left = $$str_member(VAR, 0, $$num_add($$len, -1)) # $$right(VAR, len) right = $$str_member(VAR, -$$num, -1) # $$mid(VAR, off, len) mid = $$str_member(VAR, $$off, $$num_add($$off, $$len, -1)) # $$mid(VAR, off) mid = $$str_member(VAR, $$off, -1) # $$reverse(VAR) reverse = $$str_member(VAR, -1, 0)
注意:
In these implementations, a zero
len
argument needs to be handled separately.
该函数在 Qt 5.8 引入。
Returns the number of characters in the argument.
另请参阅 size() .
该函数在 Qt 5.8 引入。
You can use this variant of the
system
function to obtain stdout from the command and assign it to a variable.
例如:
UNAME = $$system(uname -s) contains( UNAME, [lL]inux ):message( This looks like Linux ($$UNAME) to me )
像
$$cat()
,
mode
argument takes
blob
,
lines
,
true
,和
false
as value. However, the legacy word splitting rules (i.e. empty or
true
,和
false
) differ subtly.
If you pass
stsvar
, the command's exit status will be stored in that variable. If the command crashes, the status will be -1, otherwise a non-negative exit code of the command's choosing. Usually, comparing the status with zero (success) is sufficient.
See also the test variant of system() .
Converts all directory separators within
path
to separators that are compatible with the shell that is used by the
system()
functions to invoke commands. For example, slashes are converted to backslashes for the Windows shell.
该函数在 Qt 5.0 引入。
另请参阅 shell_path() .
Quotes
arg
for the shell that is used by the
system()
函数。
该函数在 Qt 5.0 引入。
另请参阅 shell_quote() .
Returns the first value of
variablename
and removes it from the source variable.
This provides convenience for implementing queues, for example.
该函数在 Qt 5.8 引入。
Returns the last value of
variablename
and removes it from the source variable.
This provides convenience for implementing stacks, for example.
该函数在 Qt 5.8 引入。
另请参阅 take_first (), last() .
Returns the list of values in
variablename
with duplicate entries removed. For example:
ARGS = 1 2 3 2 5 1 ARGS = $$unique(ARGS) #1 2 3 5
Takes an arbitrary number of arguments and converts them to upper case.
另请参阅 lower() .
Escapes the values of
variablename
in a way that enables parsing them as qmake code.
该函数在 Qt 5.0 引入。