Wednesday, 24 April 2013

How to make a custom QT project template

In this article, we will learn how to create a project template in QT creator . Why would I need custom QT project template you ask? If you work with multiple external libraries like OpenCV, QWT etc, you would have to manually add the libraries' path into the project file for every new project that requires them; this quickly becomes an inconvenience if you have a lot of projects and you don't know the configuration by heart. So lets get started.

QT Template

This tutorial is not intended to discuss QT templates in-depth for such a tutorial you would need to refer to the QT documentation. Rather, I have created a sample custom QT project template and would explain the needed alteration so you can use this to create your own template. As soon as you have gotten the sample from here and extracted the contents you should see something similar to the default QT GUI project except for wizard.xml. Before I get into what that does, let's see how to install this project template.

Installing the custom QT project template

After extracting the folder containing the project template, you would need to copy this folder into the share/qtcreator/templates/wizards/ folder in your QtCreator folder, the location of this folder will vary depending on where you installed QT. Also depending on where you installed QtCreator you might need root permission on a Linux machine before copying. Once copied, you can launch Qt creator and open the new Project wizard you should see a new project category and a new project type. selecting this new type would open a wizard which would contain the regular pages used to create a project with an extra page titled "Add External Libraries" in this sample I have included check-boxes for OpenCV and QWT selecting anyone of these would include the specified library into the created project.

How does this work?

The major job is done in wizard.xml. Most of the xml elements are self explanatory...for deeper knowledge about each element checkout the QT documentation. More importantly the <files> element allows you to specify what files you want to be added to the project by changing this you can convert the template into a console project template or any other project. Equally important is the <fields> element which allow us to add a new wizard page and throw in some check-boxes via the <field> tags. Each field has a name, a control and description.
<wizard version="1" kind="project"
        class="qt4project" firstpage="10"
        id="A.customqtproject" category="B.CustomProjects">
    <icon>console.png</icon>
    <description>Creates a custom Qt project which allows you to include external libraries.</description>
    <displayname>Custom QT Projects</displayname>;
    <displaycategory>Custom Projects</displaycategory>
    <files>
        <file source="main.cpp" openeditor="true"/>
	<file source="mainwindow.cpp" openeditor="false"/>
	<file source="mainwindow.h" openeditor="false"/>
	<file source="mainwindow.ui" openeditor="false"/>        
	<file source="project.pro" target="%ProjectName%.pro" openproject="true"/>
    </files>
    <!-- Create a 2nd wizard page with parameters -->
    <fieldpagetitle>Add External Libraries</fieldpagetitle>
    <fields>
	<field name="OPENCV">
            <fieldcontrol class="QCheckBox" truevalue="" falsevalue="# "/>
            <fielddescription>Include OpenCV Libraries</fielddescription>
        </field>
	<field name="QWT">
            <fieldcontrol class="QCheckBox" truevalue="" falsevalue="# "/>
            <fielddescription>Include QWT Libraries</fielddescription>
        </field>
    </fields>
</wizard>
A few alterations were made to the project.pro file. We append the following lines to the bottom of the default QT GUI project file.
# Opencv Library
%OPENCV%INCLUDEPATH += "/usr/local/include/opencv2"
%OPENCV%LIBS += `pkg-config --cflags --libs opencv`

# Qwt library
%QWT%CONFIG += qwt
%QWT%INCLUDEPATH +="/usr/local/qwt-6.1.0-rc3/include"
%QWT%LIBS += -L/usr/local/qwt-6.1.0-rc3/lib -lqwt
The first two lines are the configuration for an OpenCv project on my machine. You will need to change this to suit your system. Note that the configuration is preceded by name of the field for the configuration inserted between two percentage signs. To add more libraries to your template you simply add more fields to the wizard and add the configuration for that library to the .pro file preceded by the name of the controlling field.When a new project is created using this template the wizard simply checks which field is not selected and it comments that out but if it is selected it is included.

Conclusion

This tutorial has shown just one way to make a custom QT project template. I chose this method because it is typical for me to start a project with one library and half way into the project remember I need another library so with this method all I need to do is uncomment the desired library in the project file. Happy and hopefully faster coding!

No comments:

Post a Comment