The Linux Page

Find Qt version: command line, compile time, run time

Bluish smoke like design representing a modern library

Introduction

The Qt library version can be retrieved in all sorts of ways, using your command line or C++ code. There are a few I use:

Test Whether Qt is Installed

Before testing the version, you probably want to verify that Qt is installed.

qmake is pretty much always installed whenever you install Qt. Debian and Ubuntu use the qt5-default package which directly depends on qtchooser which includes qmake. Other architectures are likely to use different package names, but you certainly get a similar effect. Many script depend on qmake being installed to check the Qt version which is why in most cases it get installed automatically.

That being said, under a Linux system we can simply use the following script to determine whether Qt is installed:

if ! test -x /usr/bin/qmake
then
  # The Qt library is missing...
  echo "error: This script requires Qt to be installed."
  exit 1
fi

# The Qt library is installed
...do your thing...

This should also work under Mac/OS and other Unix systems.

Another method under Debian is to query with dpkg-query as in:

if ! dpkg-query -s qtchooser
then
  # The Qt library is missing...
  echo "error: This script requires Qt to be installed."
  exit 1
fi

# The Qt library is installed
...do your thing...

You may want to check a different package if you need specific functionality.

Note that since Qt5, the name of the package with qmake has changed. Under Debian (.deb), it is qt5-qmake and under RedHat (RPM) it is qmake-qt5.

Under MS-Windows, Qt may be installed anywhere and qmake may not be in the PATH variable. However, trying to run qmake is a solution to test whether Qt is accessible. Still, it's not a definitive answer to the problem. Short of something specific to your software, the only way to really know would be to do a search of the entire file system.

Command Line Version

The Qt tools will display the version. One of the simplest way is to use qmake as in:

qmake --version

It will tell you which version of the Qt library it is using.

When writing a configuration script, you may want to make use of the pkg-config tool (at least under Linux and Cygwin):

pkg-config --modversion QtCore

The result is just the version of the specified module. To find all the available module look in the /usr/lib/pkgconfig folder. .pc files are text files so feel free to look at them with your favority text editor (vim, right?)

Note: As of Ubuntu 16.04, the pkg-config does not seem to be defined.

Compile Time Version

At times, you want to present the Qt version you compiled your application with. This makes use of Qt macros. For example:

printf("Compiled with Qt Version %s\n", QT_VERSION_STR);

The macro is compiled at compile time and will not change, whatever happens. If you link against a compatible version, then you get the answer that corresponds to the dynamic version.

Note: the use of "%s" and the arguments is important for security reasons. Although it is unlikely that the version would include a % character, if it were, it could have disastruous effects on the printf().

Runtime Version

The version can be different at runtime than it was at compile time (i.e. you upgraded your library but not this or that tool.) In general, all 5.5.x versions are compatible between each others. You may have a few more bugs in older versions (smaller x patch version.)

To verify that you are running with a specific version, or at least version 5.5.1 (for example) then you can check the version at run time using the qVersion() function:

const char *qt_version = qVersion();

A discrepancy between the compile time and runtime version can be detected by comparing the qVersion() output to the QT_VERSION_STR. If different, you can then decide to do further testing to determine whether things will work.

if(strcmp(qt_version, QT_VERSION_STR) != 0)
{
  // version mismatch...
  // here you could verify whether just the patch is different
  // or the full blown major/minor versions
  ...
}

If you need a little handle in order to parse a version, I have a class in my wpkg project that does just that with the Debian type of versions.

debian_version.cpp
debian_version.h

Impossible Combinations

Qt libraries are generally forward compatible without much problems. Under Linux, it is also mostly backward compatible, although once in a while additional functions are added to the library and if those are used in your software, the dynamic linkage may fail.

For example, the QCoreApplication::applicationVersion() function was added in 4.4.0, if you use this function in your application, then a user with Qt 4.3.x or older version cannot start your application because the dynamic linking fails (it cannot find the applicationVersion() function as specified in your binary.)

cmake & Qt

As more and more people are using cmake instead of other tools to generate their build environment, I thought I should mention this here.

Under Linux, the cmake-data package installs a list of Qt files that help you with finding the Qt library, compiling with the Qt library, linking against the Qt library.

There is an example of the files that cmake manages:

/usr/share/cmake-3.5/Modules/DeployQt4.cmake
/usr/share/cmake-3.5/Modules/FindosgQt.cmake
/usr/share/cmake-3.5/Modules/FindQt3.cmake
/usr/share/cmake-3.5/Modules/FindQt4.cmake
/usr/share/cmake-3.5/Modules/FindQt.cmake
/usr/share/cmake-3.5/Modules/Qt4ConfigDependentSettings.cmake
/usr/share/cmake-3.5/Modules/Qt4Macros.cmake
/usr/share/cmake-3.5/Modules/UseQt4.cmake

There is an example of cmake commands to make use of Qt5 from one of my Snap! C++ projects:

find_package( Qt5Widgets       REQUIRED )
find_package( Qt5WebKit        REQUIRED )
find_package( Qt5WebKitWidgets REQUIRED )

list( APPEND QT_LIBRARIES
    Qt5::Widgets
    Qt5::WebKit
    Qt5::WebKitWidgets
)

project( snapmanagergui )

qt5_wrap_ui(SNAP_MANAGER_UIS_H
    snap-manager-mainwindow.ui
    snap-manager-aboutbox.ui
    snap-manager-createcontextbox.ui
    snap-manager-helpbox.ui
    snap-manager-initialize-websitebox.ui
    snap-manager-decode-utf8-dialog.ui
    snap-manager-connectform.ui
    snap-manager-domainform.ui
)

qt5_add_resources(SNAP_MANAGER_RESOURCES_RCC
    snap-manager-resources.qrc
)

add_executable( ${PROJECT_NAME}
    ...
    ${SNAP_MANAGER_UIS_H}
    ${SNAP_MANAGER_RESOURCES_RCC}
)

target_link_libraries( ${PROJECT_NAME}
    ...
    ${QT_LIBRARIES}
    ...
)

Extend Your Knowledge

Qt5 C++ GUI Programming Cookbook, the picture on the front page represents a stream with a fall in the background and a small lake in the front.For more about Qt, I suggest you checkout the Qt5 C++ GUI Programming Cookbook book on Amazon. It will definitely help you at work, solving many problems in your development using Qt5. Oh and remember that employers like to get you tools that allow them to reduce their costs and that includes books!

Re: Find Qt version: command line, compile time, run time

Great, thank you for the info. I made a few updates around the qmake package name. The first script is still correct, though. Once qmake is installed, you can use it as a sign that Qt is installed.

Re: Find Qt version: command line, compile time, run time

"qt5-default" is a debian-ism and doesn't exist on Arch/Fedora/openSUSE. Those also don't use "qmake" for Qt 5 but "qmake-qt5".