简体   繁体   English

如何在.pro文件中检查所选的Qt版本?

[英]How to check the selected version of Qt in a .pro file?

I have multiple versions of Qt installed, and I need to compile my project with all of them. 我安装了多个版本的Qt,我需要用所有这些版本编译我的项目。
Using a pro file, I could not find in the documentation how to do a conditional compilation. 使用专业文件,我在文档中找不到如何进行条件编译。

Ideally, this is what I would like to do: 理想情况下,这就是我想要做的事情:

QT_VERSION = 5   # this can be 4, set manually

if(QT_VERSION == 5) {
   QT += widgets
}
if(QT_VERSION == 4) {
   QT += gui
}

Naturally, the if() command does not exist in pro files. 当然,pro文件中不存在if()命令。
Is there a better way to do the same thing? 有没有更好的方法来做同样的事情?

You can use conditional functions and scopes here: 您可以在此处使用条件函数和范围:

QT_VERSION = 5   # this can be 4, set manually

equals(QT_VERSION, 5){
   QT += widgets
}
equals(QT_VERSION, 4) {
   QT += gui
}

However, there are a few things that you need to pay attention to in your original code: 但是,您需要在原始代码中注意以下几点:

  1. Explicitly defining the Qt version is not necessary, and it can make you get a headache if you forgot to change that in the .pro file. 明确定义Qt版本不是必需的,如果您忘记在.pro文件中更改它,它会让您头疼。 Instead, qmake automatically defines a variable QT_MAJOR_VERSION for you. 相反,qmake会自动为您定义变量QT_MAJOR_VERSION

  2. Using equals will work in this case. 在这种情况下,使用equals将起作用。 However, as noted below, equals performs a string comparison. 但是,如下所述, equals执行字符串比较。 However, it is better to use greaterThan and lessThan because your code will automatically stop working when you try to compile it with Qt 6 (somewhere in the future). 但是,最好使用greaterThanlessThan因为当您尝试使用Qt 6(将来的某个地方)编译它时,代码将自动停止工作。

  3. Adding gui to the QT is not needed, as it is included by default. 不需要在QT添加gui ,因为它默认包含在内。

So, your code should be: 所以,你的代码应该是:

greaterThan(QT_MAJOR_VERSION, 4) {
    QT += widgets
}

Here are some undocumented qmake gems : 以下是一些qmake宝石

  •  defined(func, type) 

    Returns true if func is defined; 如果定义了func则返回true; type must be either test or replace , to match defineTest or defineReplace . type必须是testreplace ,以匹配defineTestdefineReplace

  •  equals(var1, var) 

    (also works as isEqual ). (也适用于isEqual )。
    Returns true if var1 is equal to var2 (string comparison). 如果var1等于var2(字符串比较),则返回true。

  •  lessThan(var1, var2)` 

    Returns true if var1 is less than var2 (as an integer). 如果var1小于var2 (作为整数),则返回true。

  •  greaterThan(var1, var2) 

    Returns true if var1 is greater than var2 (as an integer). 如果var1大于var2 (作为整数),则返回true。

  •  inFile(file, var, val) 

    Returns true if a variable var is defined in the specified file. 如果在指定文件中定义了变量var则返回true。 Additionally, it can test to see if it has the requested value. 此外,它可以测试它是否具有所请求的值。

  •  load(string) 

    Something of a cross between include() and CONFIG += [feature] . include()CONFIG += [feature]之间的交叉。 load(foo) will look for a file called "foo.prf" in the standard feature path, and execute its contents immediately. load(foo)将在标准功能路径中查找名为“foo.prf”的文件,并立即执行其内容。 Features that are contained within CONFIG are executed last, after the ".pro" file has finished processing. 在“.pro”文件处理完毕后,最后执行CONFIG中包含的功能。 Like include() , it will return true if the file was found. include() ,如果找到该文件,它将返回true。

Not sure since when (Qt5 I guess), there is versionAtLeast and versionAtMost test functions. 不确定从什么时候开始(Qt5我猜),有versionAtLeastversionAtMost测试功能。

Usage example: 用法示例:

!versionAtLeast(QT_VERSION, 5.11.2):error("Use at least Qt version 5.11.2")

PS: Posting this answer, since simple googling "qmake check Qt version" doesn't brings these references (but this post does). PS:发布这个答案,因为简单的谷歌搜索“qmake检查Qt版本”没有带来这些参考(但这篇文章的确如此)。

You can make checks in one line like this: 您可以在一行中进行检查,如下所示:

equals(QT_MAJOR_VERSION, 5):!lessThan(QT_MINOR_VERSION, 5) {
   QT += bluetooth
} else {
  message(Qt $$QT_VERSION Bluetooth not supported.)
}

!lessThan there stands for greater or equal. !lessThan那么代表更大或更平等。

This is a simple test to do. 这是一个简单的测试。 This is what we have been doing in QtSerialPort and also some other modules inside the Qt framework: 这就是我们在QtSerialPort所做的以及Qt框架内的一些其他模块:

lessThan(QT_MAJOR_VERSION, 5) {
...
} else {
...
}

Similar and common conditions are: 类似和常见的条件是:

contains(QT_MAJOR_VERSION, 5): ...

or: 要么:

greaterThan(QT_MAJOR_VERSION, 4): ...

Here you can find another QtSerialPort example we have been doing in there. 在这里,您可以找到我们在那里做过的另一个QtSerialPort示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM