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:
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:
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.
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.
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.
Your directory structure and project hierarchy should look like this:
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:
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.
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