ImaginativeThinking.ca


A developers blog

How to Create a Solution in QtCreator

By: Brad

In this tutorial I’ll walk you through how to setup something like a Visual Studio Solution in QtCreator.

Download the QtQuick Sample Application.

In Visual Studio there is a concept of a Solution which is a collection of projects so that you can build the entire solution in one go which could include executable(s) and dynamic/static libraries. A solution file is just a set of configurations that tells VisualStudio what other projects should be opened and included in the build configuration and in what order each project should be built.

QtCreator does not have any notion of solution like Visual Studio does however QtCreator does have a project template that lets you create Project Hierarchy.

Creating a Project Hierarchy in QtCreator

First lets move some files around. In your Windows Exploerer go to where you downloaded and extracted the QtQuick Sample Application, the directory structure should look something like this:

QtCreator_Solution_1

Now in the root directory create a new folder and call it QtQuickSampleApp and copy everything into it (you should now have QtQuickSampleApp\QtQuickSampleApp\QtQuickSampleApp.pro). Under the first QtQuickSampleApp folder create a new plain text file called QtQuickSampleAppSolution.pro. Your directory structure should now look like this:

QtCreator_Solution_2

Open QtQuickSampleAppSolution.pro in your favorite text editor (for me its Notepad++) and paste in the following:

TEMPLATE = subdirs
SUBDIRS += QtQuickSampleApp

Here we are telling qmake to use a project template called subdirs which is the template that allows us to create essentially the same thing as a Visual Studio solution. The second line is specifying what projects to include in this solution.

Save QtQuickSampleApplicationSolution.pro and open it in QtCreator you’ll see you now have two projects one nested under the other.

QtCreator_Solution_3

Building and running the hierarchical project will render the same Calculator sample application that we’ve grown so fond of over the last few posts.

Adding a Second Project to our Hierarchy

Now that we have a hierarchical project structure we can start adding additional projects to our solution. Right-click on the root project and select New Subproject… this will bring up the New Subproject wizard. We’re going to create a Test Runner/Test Harness application so we can add Unit Tests to the QtQuick Sample Application. The Test Runner/Harness application is simply a console application so I’m going to pick Qt Console Application and call it QtQuickSampleTest.

QtCreator_TestRunner_1

As your going through the new project wizard you may notice on the summary page at the end a new drop down called Add as a subproject to project: we want to make sure the drop down has selected the root project. This will automatically append the SUBDIRS qMake variable in the QtQuickSampleApplicationSolution.pro file to include the QtQuickSampleTest project.

QtCreator_TestRunner_2

Your directory structure and project hierarchy should look like this:

QtCreator_TestRunner_3

QtCreator_TestRunner_4

Following the same process as above lets also add a Static Library project (QtQuickSampleLib) to our hierarchy so that we can share business logic between our Test Runner and Production Application. Your “Solution” should now be looking something like this:

QtCreator_TestRunner_7

For more information about how to move the buisness logic (i.e. the MyCalculatorViewModel class) from QtQuickSampleApp to QtQuickSampleLib and configure both QtQuickSampleApp and QtQuickSampleTest to use the static library see my post QTest 101: Writing UnitTests for my Qt Application

Configuring Build Order in my Solution

In our solution we have two executable projects (QtQuickSampleApp and QtQuickSampleTest) and one shared static library (QtQuickSampleLib). Both executables depend on the static library so we want to make sure that it gets built first before either of the executable projects.

To do this open QtQuickSmapleAppSolution.pro and add the following to specify that the two application projects depend on the library project.

TEMPLATE = subdirs
SUBDIRS += QtQuickSampleApp \
    QtQuickSampleTest \
    QtQuickSampleLib

QtQuickSampleApp.depends = QtQuickSampleLib
QtQuickSampleTest.depends = QtQuickSampleLib

Now in QtCreator if you select Build -> Build All you should see all three projects getting compiled where QtQuickSampleLib will be the first project built.

There you have it you can now make something similar to a Visual Studio Solution in QtCreator called a QtCreator Project Hierarchy giving you a single point of entry into a solution which could consist of multiple executables, Dynamic Linked Libraries, and/or Statically Linked Libraries.

You can download a version of the QtQuick Sample Application which has been setup in a hierarchy consisting of two executables and a statically linked library project by clicking this link.

Thank you I hope you have enjoyed and found this tutorial helpful. Feel free to leave any comments or questions you might have below and I’ll try to answer them as time permits.

Until next time think imaginatively and design creatively

Brad

My interest in computer programming started back in high school and Software Development has remained a hobby of mine ever since. I graduated as a Computer Engineering Technologist and have been working as a Software Developer for many years. I believe that software is crafted; understanding that how it is done is as important as getting it done. I enjoy the aesthetics in crafting elegant solutions to complex problems and revel in the knowledge that my code is maintainable and thus, will have longevity. I hold the designation Certified Technician (C.Tech.) with the Ontario Association of Computer Engineering Technicians and Technologists (OACETT), have been certified as a Professional Scrum Master level 1 (PSM I) and as a Professional Scrum Developer level 1 (PSD I) by Scrum.org as well as designated as an Officially Certified Qt Developer by the Qt Company. For more on my story check out the about page here

Feel free to write a reply or comment.