28

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?

László Papp
  • 51,870
  • 39
  • 111
  • 135
Pietro
  • 12,086
  • 26
  • 100
  • 193

4 Answers4

32

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. Instead, qmake automatically defines a variable QT_MAJOR_VERSION for you.

  2. 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).

  3. 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
}

Here are some undocumented qmake gems:

  • defined(func, type)
    

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

  • equals(var1, var)
    

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

  • lessThan(var1, var2)`
    

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

  • greaterThan(var1, var2)
    

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

  • inFile(file, var, val)
    

    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)
    

    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.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
10

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.

ephemerr
  • 1,833
  • 19
  • 22
10

Since Qt 5.10, there is versionAtLeast and versionAtMost test functions.

Usage example:

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

P.S.: Posting this answer, since simple googling "qmake check Qt version" doesn't brings these references (but this post does).

m7913d
  • 10,244
  • 7
  • 28
  • 56
Youw
  • 799
  • 6
  • 11
  • 4
    Needs Qt 5.10 at least. See here: https://github.com/qt/qtbase/commit/8a3e8856e5606ec616d4420ff8c4f77969432390 – FourtyTwo Jan 08 '19 at 08:22
5

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:

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.

László Papp
  • 51,870
  • 39
  • 111
  • 135