Creating custom class templates for QtCreator

Published March 08, 2012
Advertisement
The QtCreator IDE provides a set of template projects and template classes, but occasionally the need to create your own arises.


templateuc.png

QtCreator stores its templates in this directory:
..\QtSDK\QtCreator\share\qtcreator\templates\wizards\

(In that directory is a bunch of pre-existing templates, which you can look at to learn how they work. At least one of them, 'listmodel', is purely for exampling how it's done. This is how I figured it out, a few hours age - however, there is a great lack of documentation about this online, hence this journal entry)



The first step is creating a new folder for your own template under the 'wizards' directory; the name of the folder doesn't matter.

In that folder you'll want to put your template's source and header files.
QtCreator scans the 'wizards' directory looking for XML files named 'wizard.xml'. The file must be called exactly that.

A wizard.xml file looks like this:
[xml]
AdventureFar.png
Creates an inheritted GameState class for AdventureFar.
GameState
Adventure Far





GameState parameters



Class name:


[/xml]

Let's walk through it:

[xml][/xml]
kind is the type of template you are creating. It is either a 'class' template or a 'project' template.

A 'project' template creates a whole new project, with templated default files and settings.
A 'class' template creates a set of templated files for the existing project.

[xml]AdventureFar.png[/xml]
This is an (optional) 32x32 pixel icon in the same directory as your custom wizard.xml file.
It appears alongside your template's name in the QtCreator GUI.

[xml]Creates an inheritted GameState class for AdventureFar.[/xml]
This is the description of your template, in the QtCreator GUI.

[xml]Game state[/xml]
The name of the template, in the GUI.

[xml]Adventure Far[/xml]
This is the category which your template appears under, in the QtCreator GUI. (In the image above, you can see all of these)

[xml]


[/xml]
These are the files that will be added to the project by your template.
source is the template file in your template's directory.
target is the new name of the file, as it appears in your project (Ignore the funky "[color=#800000]%ClassName%.%CppSourceSuffix%[/color]", it'll be explained a bit lower). This is optional. If the 'target' name isn't in the XML file, it'll keep the same name as the source file.
openeditor - I'm not 100% sure, but I believe this opens the newly created file in the editor when the template is used.


Now, QtCreator allows your template wizards to have fairly extensive GUIs, including multiple pages and widgets and customizations. I'm only familiar with a very small subset of it.

[xml]GameState parameters[/xml]
This is the title of the GUI wizard page we are on. The wizard.xml file we're using only has a single custom page.

[xml]
...
[/xml]
fields holds a list of fields; the template I created only has a need for a single field, but your templates can easily have a half dozen or more depending on what you are wanting to do.

(Within the 'fields' tag:)


[xml]

Class name:
[/xml]
Our field's name is specified by name. The one field I needed, was the name of the class I'm having the template create for me, so my field is named 'ClassName'.

is the Qt widget that accepts user input, which will set the value of our field
The widget is specified by class (in this case, QLineEdit), and we use a RegEx validator to make sure our classname is valid. We also give the QLineEdit the default text specified by defaulttext.

is just the text that appears in the GUI to inform the user what the purpose of the field is.

[xml]
[/xml]
And that's the end of the wizard.xml file.

Now, returning upwards a bit, we had the line:

[xml][/xml]

We now see that %ClassName% will actually be replaced by the value of our field, named 'ClassName'.
%CppSourceSuffix% is a built-in field specified elsewhere in QtCreator, that lets users determine if they want to use .cpp or .cxx or whatever else they fancy.

What's important to note is that %ClassName% wont just get replaced in our wizard.xml file; it'll also get replaced in any of the files the template is generating.

Our source.cpp has code that looks like this:
%ClassName%::%ClassName%()
{


}

%ClassName%::~%ClassName%()
{


}

...creating a constructor and destructor for a class named '%ClassName%'. (Asside from our special replacement variables, our source.cpp file is just a regular C++ file.
Our header.h file also uses our field variables to create inclusion guards, like this:
#ifndef %ClassName:u%_H
#define %ClassName:u%_H


Except here, because we love uppercase inclusion guards, we use %ClassName[color=#ff0000]:u[/color]% where the '[color=#FF0000]:u[/color]' specifies to use the uppercase version. Likewise, we could use '[color=#FF0000]:l[/color]' to specify lowercase.

I'm using my template to generate empty GameState classes for my project. My game has alot of GameStates, all looking roughly the same being inherited from the same abstract class with virtual functions that need to be defined in the subclasses.

Here's how my source and header templates look.

My templated header file:
#ifndef GAME_STATE_%ClassName:u%_H
#define GAME_STATE_%ClassName:u%_H

#include "Engine/Structure/GameState.h"

#include "Common/System/System.h"

namespace Game {
namespace State {

/////////////////////////////////////////////////////////////////////////////////////

class %ClassName% : public Engine::GameState
{
public:
%ClassName%();
~%ClassName%();

void Activated();
void Deactivated();

void React(PlayerCommand &command);
void Update(float deltaTime);
void Think();
void Draw(sf::RenderTarget &renderTarget);

std::string GetName() { return "%ClassName%"; }
};

}} //End of namespaces.


#endif // GAME_STATE_%ClassName:u%_H


My templated source file:
#include "%ClassName%.%CppHeaderSuffix%"

#include "Common/Logger/Log.h"
#include "Common/Types/ConfigFile.h"

#include "Engine/Structure/GameStructure.h"

namespace Game {
namespace State {

/////////////////////////////////////////////////////////////////////////////////////

%ClassName%::%ClassName%()
{

}

%ClassName%::~%ClassName%()
{

}

void %ClassName%::Activated()
{

}

void %ClassName%::Deactivated()
{

}

void %ClassName%::React(PlayerCommand &command)
{

}

void %ClassName%::Update(float deltaTime)
{

}

void %ClassName%::Think()
{

}

void %ClassName%::Draw(sf::RenderTarget &renderTarget)
{

}

void %ClassName%::DrawOver(sf::RenderTarget &renderTarget)
{

}

/////////////////////////////////////////////////////////////////////////////////////

}} //End of namespaces.


Hopefully you'll find this useful if you use QtCreator. I found a great lack of documentation on this otherwise simple feature which is common in most IDEs.
1 likes 1 comments

Comments

Aardvajk
Cheers for that, will probably be quite handy for work.
March 08, 2012 07:11 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement
Advertisement