venerdì 17 luglio 2015

Creation of a custom project wizard in Qt Creator

Smart teams of developers are used to define a set of guidelines related to the style of the code: indentation, comments, conventions for the names of variables, functions, classes and so on.
This ensures a quick comprehension of code written by a developer to the other members of the team along the code reviews, tests, ...
Qt Creator supports this approach in some ways.
This post is the first of two about the creation of wizards in this IDE which will help you to deliver code in faster ways.
I'm going to describe a typical situation we developers are facing when we need to begin to implement a new GUI project.
The classical literature of the design patterns suggests to use the MVC approach (Model-View-Controller). The exhaustive explanation of this pattern is out-of-topic for this post but, in brief, we are going to:
1) create a Qt project: I suggest a "Qt Widgets Application" so we have, at least, a base window on which we can start to develop.
2) create a directory for each element that compounds the pattern: one directory for the Model, one for the View and one for the Controller;
3) create an additional directory called Utils that holds others classes/files that are not part of the pattern;
4) create a .pri file for each of these directory with the same name of the directory

We will see this:






5) move all the files related to the window into the View directory.
6) modify the .pro file in this way:



















7) run qmake from the menu Build. We should see:
 











8) right click the View directory and "Add existing files" to include the files related to the mainwindow










9) run the project to test if everything works (it should)
10) create an xml file int the project directory called wizard.xml:

11) provide an optional icon (see the above picture)
12) copy the entire project directory to <QtCreator install dir>\share\qtcreator\templates\wizards
13) restart Qt Creator
14) create a new project. We should see:













That's all.

This project is hosted in Github:
https://github.com/sassi67/qt4mvc

For additional informations please refer to the official documentation:
http://doc.qt.io/qtcreator/creator-project-wizards.html

In the next post I'm going to examine a wizard to create a custom wizard for a class.