Get Involved/development/Tutorials/Writing Qt Designer Plugins

From KDE Community Wiki
Revision as of 10:20, 13 September 2009 by Frinring (talk | contribs) (link to detailed qt doc)

Abstract

This tutorial shows how to add support for custom GUI elements to Qt Designer. It starts by showing how to add a simple custom widget.


Creating a simple plugin for a custom widget

We assume you have written a nice widget which you also want to be able to use in Qt Designer and the ui files. For this to achieve you have to write a plugin module for Qt Designer. It consists of just a single object of a class you have to write.

The interface to the plugin

The class, a factory, needs to be subclassed from QObject and to implement the interface QDesignerCustomWidgetInterface, as given by example for the widget MyWidget in the "mywidgetdesignerfactory.h":

  1. ifndef MYWIDGETDESIGNERFACTORY_H
  2. define MYWIDGETDESIGNERFACTORY_H

// Qt

  1. include <QtDesigner/QDesignerCustomWidgetInterface>
  2. include <QtCore/QObject>

class MyWidgetDesignerFactory : public QObject, public QDesignerCustomWidgetInterface {

 Q_OBJECT
 Q_INTERFACES( QDesignerCustomWidgetInterface )
 public:
   explicit MyWidgetDesignerFactor( QObject* parent = 0 );
 public: // QDesignerCustomWidgetInterface API
   virtual QWidget* createWidget( QWidget* parent );
   virtual QString group() const;
   virtual QIcon icon() const;
   virtual QString includeFile() const;
   virtual bool isContainer() const;
   virtual QString name() const;
   virtual QString toolTip() const;
   virtual QString whatsThis() const;

};

  1. endif

Creating the data as needed by the interface

The definition of the methods is done as shown in the file "mywidgetdesignerfactory.cpp":

  1. include "mywidgetdesignerfactory.h"

// my lib

  1. include <mywidget.h>

// Qt

  1. include <QtCore/QtPlugin>


MyWidgetDesignerFactory::MyWidgetDesignerFactory( QObject* parent )

 : QObject( parent )

{ }

QWidget* MyWidgetDesignerFactory::createWidget( QWidget* parent ) {

   MyNamespace::MyWidget* widget = new MyNamespace::MyWidget( parent );
   // init with some example data useful in the preview inside Qt Designer
   // this data will be only used there, not in the resulting view in the program.
   return widget;

}

QString MyWidgetDesignerFactory::group() const {

   return QString::fromLatin1("Some group (KDE)");

}

QIcon MyWidgetDesignerFactory::icon() const {

   return QIcon();

}

QString MyWidgetDesignerFactory::includeFile() const {

   return QString::fromLatin1("neededincludepathprefix/mywidget.h");

}

QString MyWidgetDesignerFactory::toolTip() const {

   return QString::fromLatin1("Useful Widget of Mine");

}

QString MyWidgetDesignerFactory::whatsThis() const {

   return QString::fromLatin1("Some description of my widget.");

}

bool MyWidgetDesignerFactory::isContainer() const {

   return false;

}

QString MyWidgetDesignerFactory::name() const {

   return QString::fromLatin1("MyNamespace::MyWidget");

}

// export macro, takes the name of the plugin module and the class name Q_EXPORT_PLUGIN2( mydesignerplugin, MyWidgetDesignerFactory ) For detailed information about the purpose of all the methods of the API you might want to read the related page from the Qt documentation.

Adding to the Buildsystem

After you created the two files above you need to tell the buildsystem how to build the Qt Designer plugin from them and where to install it. This needs a CMakeLists.txt file in the same directory with such content:

set( mydesignerplugin_SRCS
  mywidgetdesignerfactory.cpp
)
# the name of the plugin module is the same name as used in the macro Q_EXPORT_PLUGIN2 in the mywidgetdesignerfactory.cpp file
kde4_add_plugin( mydesignerplugin  ${mydesignerplugin_SRCS} )
target_link_libraries( mydesignerplugin
  mylib
  # other needed libs
)
install( TARGETS mydesignerplugin  DESTINATION ${PLUGIN_INSTALL_DIR}/plugins/designer )

Finding your widget in Qt Designer

If you successfully compiled and installed your plugin, (re-)start Qt Designer and have a look in the Widgetbox (on the left side by default). If everything worked perfectly you will see a new entry "MyNamespace::MyWidget" (as defined by MyWidgetDesignerFactory::name()) in the group "Some group (KDE)" (as defined by MyWidgetDesignerFactory::group()). Now drag and drop the entry to the currently edited view in Qt Designer, and voilà, your widget should be added to that view.