Thursday, April 25, 2013

Porting applications to Qt 5.x but keeping Qt 4.x compatible

I think most of you are aware that existing applications developed for Qt 4.x must be ported to be used with Qt 5.x (if not, this is worth reading). The main visible changes to do to existing C++ code are:
  1. modify your C++ files (.h and/or .cpp) so
    • #include <QtGui> changes into #include <QtWidgets>
  2. add QT += widgets towards the end of the project file (.pro)
By making such changes your code now be built using Qt 5.x But wait a minute... By making such changes your code cannot be built using Qt 4.x anymore... If you want to be able to build your applications with each of Qt versions, here's a simple trick

You must guard the code pertaining each of the Qt major version and check for the Qt version in use. For the project file (.pro) do
...
greaterThan(QT_MAJOR_VERSION, 4): QT *= widgets
...
while in your C++ code do:
#include <QtGui>
#if QT_VERSION >= 0x050000
    #include <QtWidgets>
#endif

No comments:

Post a Comment