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 ?
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 ?
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 ?Qt Code:
#if QT_VERSION >= 0x050000 // Qt5 code #else // Qt4 code #endifTo copy to clipboard, switch view to plain text mode
qmake Code:
greaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTo copy to clipboard, switch view to plain text mode
drhex (15th March 2013), nils_heidorn (25th November 2014)
It appears one needs to
first, or else QT_VERSION won't be defined.
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:
not thisQt Code:
#include <QApplication> #include <QMainWindow> #include <QTableView>To copy to clipboard, switch view to plain text mode
The entry in the PRO file is needed either way.Qt Code:
#include "qglobal.h" #if QT_VERSION >= 0x050000 // Qt5 code #include <QtWidgets> #else // Qt4 code #include <QtGui> #endifTo copy to clipboard, switch view to plain text mode
Last edited by ChrisW67; 16th March 2013 at 23:44.
Bookmarks