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.


Posted

in

by

Tags:

Comments

One response to “Custom Class wizards in Qt Creator”

  1. Thomas Meyer Avatar
    Thomas Meyer

    Hello,
    do you know, if it is possible to add a 3rd (or more) wizard page to a custom wizard? (your update on the top, Adding New Custom Wizards)
    I have tried it with a addition with ” and so on in the ‘wizard.xml’, but the result is a merge in one page.