Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check QT_VERSION to include different header?

Tags:

c++

qt

Qt4 and Qt5 have different organization of header files. So I need to check the qt version to include different things, for example:

#if QT_VERSION >= 0x050000
    #include <QtMultimedia>
#endif

however, this seems does not work. The QT_VERSION has not been defined. How can I solve this problem?

like image 906
Wang Avatar asked Jul 22 '14 23:07

Wang


2 Answers

As soon as you include <QtGlobal> you can check that with macros:

#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)

It's the same as @Javier suggested, just a different, more readable to me, syntax

like image 192
rightaway717 Avatar answered Oct 08 '22 10:10

rightaway717


As @Tay2510 commented, QT_VERSION is declared inside of <QtGlobal>. As a result, you must type #include <QtGlobal> or another header that includes <QtGlobal>, before the version dependent preprocessor directives.

<QObject> and <QCoreApplication> may also be used to access QT_VERSION.

Here is an example of the code:

#include <QtGlobal>
#if QT_VERSION >= 0x050000
    #include <QApplication>
#else
    #include <QtGui/QApplication>
#endif

Remember that the qmake project should also handle version variations. Here are changes that should be done when using the QT multimedia library:

The .pro file should look something like:

QT += core gui

greaterThan(4, QT_MAJOR_VERSION): QT += widgets multimedia
lessThan(5, QT_MAJOR_VERSION): CONFIG += mobility
lessThan(5, QT_MAJOR_VERSION): MOBILITY += multimedia
like image 21
Javier Cordero Avatar answered Oct 08 '22 12:10

Javier Cordero