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
}
Naturally, the if() command does not exist in pro files.
Is there a better way to do the same thing?
You can make checks in one line like this:
!lessThan
there stands for greater or equal.You can use conditional functions and scopes here:
However, there are a few things that you need to pay attention to in your original code:
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. Instead, qmake automatically defines a variable
QT_MAJOR_VERSION
for you.Using
equals
will work in this case. However, as noted below,equals
performs a string comparison. However, it is better to usegreaterThan
andlessThan
because your code will automatically stop working when you try to compile it with Qt 6 (somewhere in the future).Adding
gui
to theQT
is not needed, as it is included by default.So, your code should be:
Here are some undocumented
qmake
gems:Returns true if
func
is defined; type must be eithertest
orreplace
, to matchdefineTest
ordefineReplace
.(also works as
isEqual
).Returns true if var1 is equal to var2 (string comparison).
Returns true if
var1
is less thanvar2
(as an integer).Returns true if
var1
is greater thanvar2
(as an integer).Returns true if a variable
var
is defined in the specified file. Additionally, it can test to see if it has the requested value.Something of a cross between
include()
andCONFIG += [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 withinCONFIG
are executed last, after the ".pro" file has finished processing. Likeinclude()
, it will return true if the file was found.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:
Similar and common conditions are:
or:
Here you can find another QtSerialPort example we have been doing in there.
Not sure since when (Qt5 I guess), there is versionAtLeast and versionAtMost test functions.
Usage example:
P.S.: Posting this answer, since simple googling "qmake check Qt version" doesn't brings these references (but this post does).