Qt with Visual Studio Express – 1

As a student I have access to the standard editions of VisualStudio for free (MSDNAA). However, I am working with some people that just have the Express versions of Visual Studio.
When we decided to start using Qt, we faced the challenge that Qt plugin cannot be used with the Express editions of Visual Studio. So I tried to get Qt working for Visual Studio Express 2013 as good as possible.

To get things running we used a step-by-step guide written by “Kabilen”. It was written for Qt4, but it also works with Qt5.
Using this guide, you can now compile projects using Qt in Visual Studio Express.

We added one additional step to setup our project: In the linked guide the QT directory is accessed using an environment variable QTDIR. We also setup this variable on our machines, since from time to time such variable is quite helpful. However, when this environment variable is accessed in the VisualStudio project, one looses the ability to use different versions of Qt in different projects on the same machine. Luckily VisualStudio offers the perfect solution for this problem: We already used a property sheet to store all machine/user-dependent paths. So we extended the property sheet to include a new user macro ‘QTDIR’ which can be used during the build process.

However, it is still very complex to add new Qt resources to the Visual Studio project.

The first thing we wanted to be able to do, is to add new Qt dialogs to our project. So I analyzed what the Qt plugin was doing when adding a new dialog:
– A .ui, .h and a .cpp file for the dialog is created and added to the project
– For the .h and .ui files, custom build events are created.
— For the .h file of the dialog, a custom build event creates a moc_dialogname.cpp file in the “GeneratedFiles/ConfigurationName” folder with the moc tool. ConfigurationName is replaced with the active configuration of the build, e.g. Release or Debug. It is used, because the moc tool destinguishes between a debug and a release mode.
— For the .ui file a custom build event is generated that creates a ui_dialogname.h file in a “GeneratedFiles” folder using the uic tool from Qt
– All mentioned files are added to the build process and into the VisualStudio solution. The files in the “GeneratedFiles/ConfigurationName” folder, are just used in build of the respective configuration. E.g. files in GeneratedFiles/Debug are excluded from Release builds.

All the steps can also be done using VisualStudio Express without any further utilities. However, it is a very cumbersome and error-prone process. Therefor I decided to write a powershell script that automates all this steps

The powershell script expects the name of the new dialog as parameter -dialogname, otherwise it asks for it. Additionally one can also specify a -project parameter. In our case we do not really use this parameter, the default value set in the QtProject.ps1 script is just set to the needed value.

The script uses 3 template files for the .ui, .h and .cpp files to create. These files can contain arbitrary code, the string ‘TEMPLATE_DIALOG_NAME’ is replaced with the name of the dialog to create. The content of the template files with the replaced dialog name is then written to a dialog directory ($dialogDir) in the project directory.

Afterwards the $project.vxproj and $project.vcproj.filters file are modified: In the .vcxproj file all the necessary build events and files to include to the build are added. In the .filters file all generated files that should not be modified by hand are added into one filter. This filter contains children for all different build configurations. The other three files (.cpp, .h and .ui) are added into three other filters.

We add the ui_dialogname.h file to the filter ‘Generated Files’, the moc_dialogname.cpp for the debug version into ‘Generated Files/Debug’ and the moc_dialogname.cpp for the release version into ‘Generated Files/Release’. For the source files we use ‘Qt\Source Files’, ‘Qt\Header Files’ and ‘Qt\Forms’

The script is available as initial version at GitHub. There is also a README file available which contains instructions how to setup and use the script for your own project.

As we are moving on using Qt, I plan to publish other utilities as soon as I see the need for it. I think the next step will be a utility for translation files.

This entry was posted in Development and tagged , , , . Bookmark the permalink.