Cross Qt Development!

It’s been ten months since Qt 5 has been released. The first official version was a disastrous one! 5.0.1 was a little bit better, 5.1.0 was good enough, and now we have 5.1.1 that is a great one.  As a Qt developer it’s not so hard to get used to the new version. There is a comprehensive guide here  to help developers to port their codebase from 4th to 5th Qt.

The same thing is now happening to me now. Sooner or later, I will have to port my applications to Qt5. So what’s I’m waiting for? Oh no… Backward Compatibility of The Source in Code! This makes things a little bit hard for me. Because I make and deploy my applications on Windows using latest version of Qt, but same products rely on operating system version of the Qt libraries in Linux machines. Most distributions still keep Qt4 in their repositories as main version of Qt. Obviously because of huge list of stuff that depend on 4th Qt.

So it looks best solution for me is porting the code base into a median state that the code can be compiled with both Qt 4 and 5 libraries. That sounds pretty boring. Actually it is.

I will use same approach with my previous post (Cross Platform Development). So the first rule is:

I. If there exists a way (W) to do task (T) that is compatible with both 4th and 5th Qt, use it, unless it’s  more complicated than collective complexity of Qt4-specific code (W4) and  Qt5-specific code (W5). Mathematically speaking,

if( W < W4+ W5) {;
else if (W > W4 + W5) {

Rule number two is obvious result of rule number one.

If there is no common code between Qt4 and 5, write both codes and choose which one to compile at compile time.

Let me show an example from Qtz project. In project files I have to take care of changes in module system. Fortunately there exist qmake macros that detect Qt version. More than this, there exist conditional directives that can be used for detecting Qt version.

QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 4): CONFIG   += C++11
lessThan(QT_MAJOR_VERSION, 5): QMAKE_CXXFLAGS += -std=c++0x

Same rule applies in source code:

CandleGraphicsItem::CandleGraphicsItem(const Candle& candle, QGraphicsItem *parent) :
    QGraphicsItem(parent), candle_(candle)
#if QT_VERSION >= 0x050000

Using QT_VERSION macro from <QtGlobal> header, you can detect which Qt version is used to compile your code. That’s cool 🙂 Now I can compile same code with every version of Qt since 4.6 to 5.1.1.

For a detailed description on Qt4 and Qt5 read this article by KDAB guys. They have also developed an automated script to convert codes from 4th to 5th Qt.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s