Jump to content

  • Log In with Google      Sign In   
  • Create Account






Creating custom class templates for QtCreator

Posted by Servant of the Lord, in Article, Tool 07 March 2012 · 1,969 views

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


Posted Image

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:
<wizard kind="class">
<icon>AdventureFar.png</icon>
<description>Creates an inheritted GameState class for AdventureFar.</description>
<displayname>GameState</displayname>
<displaycategory>Adventure Far</displaycategory>
<files>
<file source="source.cpp" target="%ClassName%.%CppSourceSuffix%" openeditor="true"/>
<file source="header.h" target="%ClassName%.%CppHeaderSuffix%" openeditor="true"/>
</files>
<!-- Create parameter wizard page -->
<fieldpagetitle>GameState parameters</fieldpagetitle>
<fields>
<field name="ClassName">
<fieldcontrol class="QLineEdit" validator="^[a-zA-Z0-9_]+$" defaulttext="MyGameState" />
<fielddescription>Class name:</fielddescription>
</field>
</fields>
</wizard>


Let's walk through it:

<wizard kind="class">

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.

<icon>AdventureFar.png</icon>

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.

<description>Creates an inheritted GameState class for AdventureFar.</description>

This is the description of your template, in the QtCreator GUI.

<displayname>Game state</displayname>

The name of the template, in the GUI.

<displaycategory>Adventure Far</displaycategory>

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

<files>
<file source="source.cpp" target="%ClassName%.%CppSourceSuffix%" openeditor="true"/>
<file source="header.h" target="%ClassName%.%CppHeaderSuffix%" openeditor="true"/>
</files>

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 "%ClassName%.%CppSourceSuffix%", 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.

<fieldpagetitle>GameState parameters</fieldpagetitle>

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.

<fields>
...
</fields>

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:)
<field name="ClassName">
<fieldcontrol class="QLineEdit" validator="^[a-zA-Z0-9_]+$" defaulttext="MyGameState" />
<fielddescription>Class name:</fielddescription>
</field>

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'.

<fieldcontrol> 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.

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

</wizard>

And that's the end of the wizard.xml file.

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

<file source="source.cpp" target="%ClassName%.%CppSourceSuffix%"  openeditor="true"/>


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:u% where the ':u' specifies to use the uppercase version. Likewise, we could use ':l' 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.




Cheers for that, will probably be quite handy for work.

Latest Visitors

PARTNERS