Migrating your project from Qt4 to Qt5

I worked on a hobby project called MMapper back in graduate school that served as my escape from the daily grind. This project is pretty much responsible for my career as an engineer due to how much fun I had hacking on it. I fondly remember how ecstatic I was combining my love for gaming, playing the role as product manager and engineer, and building something useful for my users/fellow gamers.

Unfortunately, I abandoned my project back when I started my day job and the years have not been kind to it. I had always meant to migrate the source code from Sourceforge to GitHub and also remove some technical debt by upgrading it to the latest version of Qt but something else would always get in the way. I sat down back in September and decided that enough was enough! Little did I know that it would take more than a weekend to finally get it all working…

I figured my first step would be to set up my Qt developer environment again. After 4 long years I downloaded and installed the Qt development libraries and CMake build system once again.I fully expected some version breakage between the major versions of Qt but was surprised to find that CMake refused to even build my project!

CMake went through some pretty radical changes between versions 2.6 and 2.8 when they tried to streamline how they built Qt packages. I wasn’t able to find any sort of documentation on how to migrate a CMake build from Qt4 to Qt5 other than this post which only hinted at which direction to go in. After going through a lot of documentation and guess work I have eventually figured out that the old commands that we used to have to manually run (like QT4_WRAP_CPP) have been done away with newer fancier modules that do these things automatically. I eventually got CMake to actually start building the code (if you want to see how I accomplished this take a look at this commit).

I was then greeted with the expected missing function compilation errors that occur between major API versions. I followed the Qt 4.x to Qt5 transition wiki and easily fixed these compile time errors by migrating to the newer APIs. My project finally was able to compile and run!

The next step was packaging which meant I had to work with CMake once again but this time with CPack. I had initially migrated from Qt’s qmake system for the sole purpose of automating the creation of installers around MMapper using CPack. This worked great with Qt4 where I was able to create rpms, source tgzs, and both Windows and Mac installers with ease. Now days it’s a pretty different story and Qt provides the Qt Installer Framework which probably is much easier to use.

My main difficulty with packaging was including the necessary dynamic libraries that are dependencies of Qt. There has been quite a bit of regression and bloat with time and the number of libraries that are now required to be packaged for Windows are well over 20MBs in size and have quadrupled in count. This lead me to try to resort to alternative solutions where I ended up trying to statically compile my Qt5 libraries for shipment. This took me 4 months because of various bugs that held me up until Qt 5.4 was released. Instead, I eventually just stuck with dynamically linking everything and using the new windeployqt tool that was added in Qt5. Take a look at this commit to see how I used that to eventually figure out which dependencies are required for my project.

Happy hacking!

12/10/2015: A year after this post, I ended up migrating QGLWidget to QOpenGLWidget due to some bugs. Read about that here.