问题
How can you test if a variable is empty or not defined in a qmake
.pro
file? I want to
be able to set up a default value if the variable is not defined.
I tried
eval("VARIABLE" = ""){
VARIABLE = test
}
eval("VARIABLE" = ""){
message(variable is empty)
}
but I still get the message "variable is empty".
回答1:
there is already the function isEmpty I didn't spot:
isEmpty(VARIABLE){
VARIABLE = test
}
isEmpty(VARIABLE ){
message(variable is empty)
}
I don't understand why eval didnt work thought...
回答2:
Like your own answer says, isEmpty(VARIABLE)
does what you want:
isEmpty(VARIABLE) {
...
}
The qmake language has no equivalent of an equals operator (==
), but you can compare things like this:
equals(VARIABLE, foo) {
...
}
You can also check if a variable contains a substring, using a regular expression:
contains(VARIABLE, .*foo.*) {
...
}
The reason why eval()
didn't work, is that it executes the statement within it and returns true if the statement succeeded.
So by doing this:
eval(VARIABLE = "") {
...
}
...you are actually assigning ""
to VARIABLE
, making the variable empty and entering the block.
More about test functions: http://qt-project.org/doc/qt-5/qmake-test-function-reference.html
回答3:
if test is a string value, try
eval("VARIABLE" = ""){
VARIABLE = "test"
}
and if test is another variable, try
eval("VARIABLE" = ""){
VARIABLE = $$test
}
来源:https://stackoverflow.com/questions/13803893/test-if-a-variable-is-empty-and-give-default-value-during-qmake