如何在 .pro 文件中检查选定的 Qt 版本? [英] How to check the selected version of Qt in a .pro file?

查看:116
本文介绍了如何在 .pro 文件中检查选定的 Qt 版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了多个版本的 Qt,我需要用所有版本编译我的项目.
使用 pro 文件,我在文档中找不到如何进行条件编译.

I have multiple versions of Qt installed, and I need to compile my project with all of them.
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
}

当然,pro 文件中不存在 if() 命令.
有没有更好的方法来做同样的事情?

Naturally, the if() command does not exist in pro files.
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. 没有必要明确定义 Qt 版本,如果您忘记在 .pro 文件中更改它,可能会让您头疼.相反,qmake 会自动为您定义一个变量 QT_MAJOR_VERSION.

在这种情况下使用 equals 将起作用.但是,如下所述,equals 执行字符串比较.但是,最好使用 greaterThanlessThan,因为当您尝试使用 Qt 6(将来的某个地方)编译时,您的代码将自动停止工作.

Using equals will work in this case. However, as noted below, equals performs a string comparison. 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).

不需要将 gui 添加到 QT 中,因为它是默认包含的.

Adding gui to the QT is not needed, as it is included by default.

因此,您的代码应该是:

So, your code should be:

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

<小时>

这里有一些未记录的qmake gems:

defined(func, type)

如果定义了func,则返回真;类型必须是 testreplace,以匹配 defineTestdefineReplace.

Returns true if func is defined; type must be either test or replace, to match defineTest or defineReplace.

equals(var1, var)

(也可用作 isEqual).
如果 var1 等于 var2(字符串比较),则返回 true.

(also works as isEqual).
Returns true if var1 is equal to var2 (string comparison).

lessThan(var1, var2)`

如果 var1 小于 var2(作为整数),则返回 true.

Returns true if var1 is less than var2 (as an integer).

greaterThan(var1, var2)

如果 var1 大于 var2(作为整数),则返回 true.

Returns true if var1 is greater than var2 (as an integer).

inFile(file, var, val)

如果在指定文件中定义了变量 var,则返回 true.此外,它还可以测试它是否具有请求的值.

Returns true if a variable var is defined in the specified file. Additionally, it can test to see if it has the requested value.

load(string)

include()CONFIG += [feature] 之间的交叉.load(foo) 将在标准特征路径中查找名为foo.prf"的文件,并立即执行其内容.CONFIG 中包含的功能在.pro"文件完成处理后最后执行.与 include() 一样,如果找到文件,它将返回 true.

Something of a cross between include() and CONFIG += [feature]. load(foo) will look for a file called "foo.prf" in the standard feature path, and execute its contents immediately. Features that are contained within CONFIG are executed last, after the ".pro" file has finished processing. Like include(), it will return true if the file was found.

这篇关于如何在 .pro 文件中检查选定的 Qt 版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆