c++qt

how to check QT_VERSION to include different header?


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?


Solution

  • 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