PDA

View Full Version : compiling under qt4 AND qt5



drhex
14th March 2013, 18:03
In Qt 4.x, one typically needs to #include <QtGui>, and in Qt 5.x much of the stuff there is moved to <QtWidgets>

What are the recommended ifdefs needed in c++ source code and .pro -files in order to have a project compile under both Qt4 and Qt5 ?

wysota
14th March 2013, 20:16
In Qt 4.x, one typically needs to #include <QtGui>, and in Qt 5.x much of the stuff there is moved to <QtWidgets>
Actually you should never use the two above because this significatly increases compile time. You should always include just the classes you need


What are the recommended ifdefs needed in c++ source code and .pro -files in order to have a project compile under both Qt4 and Qt5 ?


#if QT_VERSION >= 0x050000
// Qt5 code
#else
// Qt4 code
#endif

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

drhex
16th March 2013, 21:12
It appears one needs to

#include <qglobal.h>

first, or else QT_VERSION won't be defined.

ChrisW67
16th March 2013, 23:37
If you take wysota's advice about individual class #includes there will usually be no reason to #ifdef between Qt4 and Qt5 in your code. Even if there is a need for version based code the first #include <QApplication>, <QWidget> etc. will have brought qglobal.h in anyway.

This:


#include <QApplication>
#include <QMainWindow>
#include <QTableView>

not this


#include "qglobal.h"
#if QT_VERSION >= 0x050000
// Qt5 code
#include <QtWidgets>
#else
// Qt4 code
#include <QtGui>
#endif

The entry in the PRO file is needed either way.