I need to include the following header in my code:
#include <5.4.1/QtGui/private/qzipwriter_p.h>
The problem is, we are compiling this on other Qt versions, such as 5.4.2 or 5.5.x
I know I should not use these "private" headers since they are unsupported, but we need them at least until we have a durable replacement.
How can I concatenate the QT_VERSION_STR
variable in the path so that this works with all versions?
You can compare numeric QT_VERSION
macro using human readable helper QT_VERSION_CHECK
that combines major, minor and patch numbers into QT_VERSION
format:
#if QT_VERSION == QT_VERSION_CHECK(5, 4, 1)
//...
#endif
The idea for concatenation taken from GCC headers C Macro - Dynamic #include
The problem is that we do not have macro tokens with Qt major, minor and patch versions. There is only numeric QT_VERSION
. So, it is tricky to get required numbers. It is possible to transfer them as macro definitions from .pro
file using qmake
variables QT_*_VERSION
:
DEFINES += QT_MAJOR_VERSION=$$QT_MAJOR_VERSION
DEFINES += QT_MINOR_VERSION=$$QT_MINOR_VERSION
DEFINES += QT_PATCH_VERSION=$$QT_PATCH_VERSION
Now those macro versions can be used in source files:
// To return as a string: "5.4.1/QtGui/private/qzipwriter_p.h"
#define qt_header__(x) #x
#define qt_header_(major,minor,patch) qt_header__(major.minor.patch/QtGui/private/qzipwriter_p.h)
#define qt_header(major,minor,patch) qt_header_(major,minor,patch)
// Simpler without stringification, however Qt Creator cannot follow
// that header
#define qt_header(major,minor,patch) <major.minor.patch/QtGui/private/qzipwriter_p.h>
#include qt_header(QT_MAJOR_VERSION, QT_MINOR_VERSION, QT_PATCH_VERSION)
It is better to use stringified variant ("5.4.1/QtGui/private/qzipwriter_p.h"
). In that case the latest versions of Qt Creator can follow such qt_header()
macro and highlight text accordingly.