Custom Class wizards in Qt Creator

[Update – 25th April 2011: Qt Creator 2.0+ has a much better support for custom class and/or project wizards here: http://doc.qt.nokia.com/qtcreator-snapshot/creator-project-wizards.html]

Qt Creator comes built in with a set of project/class/file wizards. These wizards are great to get started with, but I always wished if there were an easy way to add support for more project/file/class wizards. In our projects we frequently write models, delegates and widgets. And the skeleton (or starting point) for most classes look the same. So if there was an easy way to quickly create skeleton for a model/delegate/widget class it would be very useful. With this in mind, I started working on the HintBasedWizard plugin for Qt Creator. You can download the source code by checking out the code from the following SVN location

SVN Location: https://svn2.hosted-projects.com/vcreatelogic/VCLTools/QtCPlugins
Username: anonymous
Password: anonymous

Right now we only have support for class wizards based on hints. The hints are provided in three files

  • Header hint
  • Source hint
  • Description hint

The header-hint for item-model class looks like this (and is saved in a file called ItemModelHeader)

#ifndef {{UPPER_CLASS_NAME}}_H
#define {{UPPER_CLASS_NAME}}_H

#include 

struct {{CLASS_NAME}}Data;
class {{CLASS_NAME}} : public QAbstractItemModel
{
Q_OBJECT

public:
{{CLASS_NAME}}(QObject* parent=0);
~{{CLASS_NAME}}();

int rowCount(const QModelIndex& parent=QModelIndex()) const;
int columnCount(const QModelIndex& parent=QModelIndex()) const;
QVariant data(const QModelIndex& index, int role=Qt::DisplayRole) const;
QModelIndex index(int row, int column, const QModelIndex& parent=QModelIndex()) const;
QModelIndex parent(const QModelIndex& index) const;

private:
{{CLASS_NAME}}Data* d;
};

#endif // {{UPPER_CLASS_NAME}}_H

The source-hint for the same looks like this (and is stored in a file called ItemModelSource)

#include "{{H_FILE_NAME}}"

struct {{CLASS_NAME}}Data
{

};

{{CLASS_NAME}}::{{CLASS_NAME}}(QObject* parent)
:QAbstractItemModel(parent)
{
d = new {{CLASS_NAME}}Data;
}

{{CLASS_NAME}}::~{{CLASS_NAME}}()
{
delete d;
}

int {{CLASS_NAME}}::rowCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);

return 0;
}

int {{CLASS_NAME}}::columnCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);

return 0;
}

QVariant {{CLASS_NAME}}::data(const QModelIndex& index, int role) const
{
Q_UNUSED(index);
Q_UNUSED(role);

return QVariant();
}

QModelIndex {{CLASS_NAME}}::index(int row, int column, const QModelIndex& parent) const
{
Q_UNUSED(row);
Q_UNUSED(column);
Q_UNUSED(parent);

return QModelIndex();
}

QModelIndex {{CLASS_NAME}}::parent(const QModelIndex& index) const
{
Q_UNUSED(index);

return QModelIndex();
}

The description-hint, which is an XML file, for the same looks like this (and is stored in a file called ItemModelClassHint.xml)

<hint>

<!-- Meta information about the hints -->
<title>QAbstractItemModel class</title>
<type>class</type>
<name>Model class</name>
<category>Qt Classes</category>
<description>Generates a QAbstractItemModel implementation</description>
<icon></icon>

<!-- actual hints file -->
<header>ItemModelHeader</header>
<implementation>ItemModelSource</implementation>

<!-- hint fields to ask -->
<field name="CLASS_NAME" label="Class name">ItemModel</field>

</hint>

I store these files in a folder and launch Qt Creator (with the HintBasedWizard plugin compiled/installed ofcourse). Go to Tools -> Options -> Hints Configuration page and add the newly created hint-description file (ItemModelClassHint.xml) by clicking on the “Add Class Hint” button.

Click on Ok. Now start a new project and try adding a new class using the “File-> New” class wizard.

Select the “QAbstractItemModel class” class under Qt Classes and click “Ok”. In the wizard that shows up next, enter appropriate values.

Notice how default values are picked up from the text for <field name=”CLASS_NAME”> xml element in ItemModelClassHint.xml file. Click on Next and notice how the class gets generated for you within your project.

I can now create as many class-hints as I want and increase my productivity in programming. The HintBasedWizard plugin comes with class-wizards for the following class types

  • QAbstractItemModel
  • QWidget
  • QAbstractItemDelegate and
  • QGraphicsItem

We plan to add more class-hints in the coming days and also provide support for hint based project and file wizards.

Expand Declarations in Qt Creator

One of the things I dont particularly enjoy is to transfer functions from .h (declaration) to .cpp (implementation), specifying the class scope and then creating open and close brackets. The whole process consumes time and I always thought that it can be automated quite easily in Qt Creator. Just wrote a plugin for Qt Creator that makes it possible.

Consider the following class definition below.

Select the function declarations from constructor all the way down to setCurrentColor(). Copy the code snippet using Ctrl+C. We now have a .cpp file for this class as shown below

With the ExpandDeclarations plugin installed, we can now hit the key-combination Ctrl+Shift+V, to get this dialog box

to get this

This one really saves time, I tell you.

You can grab a copy of the plugin from here: https://svn2.hosted-projects.com/vcreatelogic/VCLTools/QtCPlugins/. Username and password for read-only access is anonymous/anonymous. In the same location you can also find a plugin that provides a project wizard called “Qt Creator Plugin” that automatically generates a Qt Creator Plugin skeleton project for you.

You can now select File -> New project and select the “Qt Creator Plugin” project.

Answer some simple questions

And have a fully functional empty plugin project automatically generated :-).

These plugins are given away under LGPL. So you can use them for any purpose you see fit.