test if a variable is empty and give default value during qmake

时光总嘲笑我的痴心妄想 提交于 2019-12-05 19:31:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!