How do I change which version of Qt is used for qmake?

This isn't necessarily Unix/Linux specific, so you are probably better asking this on Stack Overflow. Never the less, QtCreator is usually quite good at detecting alternative Qt installs, just create a new project and look under the Projects tab on the left. You can set different build configurations there. There should be a drop down box to select from the various installed versions for each configuration.

Otherwise, it seems to the QTDIR environmental variable is used to set the Qt version. By default QtCreator sets this to /usr/share/qt4 for me, so setting it to the equivalent path (ie the one where the configuration files are) should make qmake build with a different version. You could test with something like:

QTDIR=/usr/share/qtX qmake --version

It may also be possible to set this in the .pro file, but if so it is undocumented (as with quite a lot of qmake variables).

Also, if you want to build with a specific major version, qmake is usually just symlinked to a binary for the default major version. The real binaries are qmake-qt4, qmake-qt5 etc. Also see man qtchooser and the qtX-default packages on Debian based systems.

Update

There is a bug with qtchooser on Ubuntu 13.04 and 13.10 which seem to affect the way Qt applications detect different Qt versions, see https://bugs.launchpad.net/ubuntu/+source/qt4-x11/+bug/1177823. This may affect QtCreator also.


It helped me to use -qt=qt5 switch or QT_SELECT=qt5 environment variable.

$ qmake --version
QMake version 2.01a
Using Qt version 4.8.7 in /usr/lib/x86_64-linux-gnu

$ qmake -qt=qt5 --version
QMake version 3.0
Using Qt version 5.5.1 in /usr/lib/x86_64-linux-gnu

$ QT_SELECT=qt5 qmake --version
QMake version 3.0
Using Qt version 5.5.1 in /usr/lib/x86_64-linux-gnu

There is a better method.

If you want to make your changes permanent, you need to modify the /usr/lib/x86_64-linux-gnu/qt-default/qtchooser/default.conf file, which is a symlink to ../../../../share/qtchooser/qt4-x86_64-linux-gnu.conf.

Here is an example for my system (Ubuntu 17.10 x64, Qt 5.10.1). I would suggest to keep both original symlink file and its target in place (in case you want to recover the original configuration). And also create the new files in the default locations (for consistency). So here are the steps:

Rename the symlink file:

sudo mv /usr/lib/x86_64-linux-gnu/qt-default/qtchooser/default.conf /usr/lib/x86_64-linux-gnu/qt-default/qtchooser/default.conf_orig

Create a new target configuration file (with any name):

gksudo gedit /usr/share/qtchooser/my_Qt_5.10.1_Desktop_gcc_x64.conf

This file must contain two lines: the first line is the path to the Qt binaries (including qmake) and the second is the path to the Qt libraries (including .so files). In my case it is

<Qt_dir>/5.10.1/gcc_64/bin
<Qt_dir>/5.10.1/gcc_64/lib

Save it and close it. Create a symlink default.conf to the new configuration file:

ln -s /usr/share/qtchooser/my_Qt_5.10.1_Destop_gcc_x64.conf /usr/lib/x86_64-linux-gnu/qt-default/qtchooser/default.conf

Check your Qt version:

qmake --version

Now it should always use the specified version.

Tags:

Qt

Make

Qtcreator