Get Involved/development/Tutorials/Writing Qt Designer Plugins: Difference between revisions

From KDE Community Wiki
No edit summary
(Proposed_deletion)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Proposed_deletion|reason=Uses the website qt.nokia.com. The page https://community.kde.org/Get_Involved/development/Tutorials and its children should not exist.}}
{{Review}}
== Abstract ==
== 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.
This tutorial shows how to add support for custom GUI elements to Qt Designer. In the first section you learn how to write a Qt Designer plugin for a simple custom widget. The second section then presents how to support more than one widget in a plugin.
 


== Creating a simple plugin for a 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.  
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 Interface to the Plugin ===
The class, a factory, needs to be subclassed from {{qt|QObject}} and to implement the interface {{qt|QDesignerCustomWidgetInterface}}, as given by example for the widget MyWidget in the "mywidgetdesignerfactory.h":
The class, a factory, needs to be subclassed from {{qt|QObject}} and to implement the interface {{qt|QDesignerCustomWidgetInterface}}, as given by example for the widget MyWidget in the file "mywidgetdesignerfactory.h":


<code cppqt n>
<syntaxhighlight lang="cpp-qt" line="">
#ifndef MYWIDGETDESIGNERFACTORY_H
#ifndef MYWIDGETDESIGNERFACTORY_H
#define MYWIDGETDESIGNERFACTORY_H
#define MYWIDGETDESIGNERFACTORY_H
Line 25: Line 28:


   public:
   public:
     explicit MyWidgetDesignerFactor( QObject* parent = 0 );
     explicit MyWidgetDesignerFactory( QObject* parent = 0 );


   public: // QDesignerCustomWidgetInterface API
   public: // QDesignerCustomWidgetInterface API
Line 39: Line 42:


#endif
#endif
</code>
</syntaxhighlight>


=== Creating the data as needed by the interface ===
=== Creating the Data as needed by the Interface ===


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


<code cppqt n>
<syntaxhighlight lang="cpp-qt" line="">
#include "mywidgetdesignerfactory.h"
#include "mywidgetdesignerfactory.h"


Line 104: Line 107:
// export macro, takes the name of the plugin module and the class name
// export macro, takes the name of the plugin module and the class name
Q_EXPORT_PLUGIN2( mydesignerplugin, MyWidgetDesignerFactory )
Q_EXPORT_PLUGIN2( mydesignerplugin, MyWidgetDesignerFactory )
</code>
</syntaxhighlight>
 
For detailed information about the purpose of all the methods of the API you might want to read [http://qt.nokia.com/doc/designer-creating-custom-widgets.html the related page] from the Qt documentation.
For detailed information about the purpose of all the methods of the API you might want to read [http://qt.nokia.com/doc/designer-creating-custom-widgets.html the related page] from the Qt documentation.


Line 111: Line 115:
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:
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
<syntaxhighlight lang="cmake">
  mywidgetdesignerfactory.cpp
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
# the name of the plugin module is the same name as used in the macro Q_EXPORT_PLUGIN2 in the file mywidgetdesignerfactory.cpp
kde4_add_plugin( mydesignerplugin  ${mydesignerplugin_SRCS} )
kde4_add_plugin( mydesignerplugin  ${mydesignerplugin_SRCS} )


target_link_libraries( mydesignerplugin
target_link_libraries( mydesignerplugin
  mylib
  mylib
  # other needed libs
  # other needed libs
)
)


install( TARGETS mydesignerplugin  DESTINATION ${PLUGIN_INSTALL_DIR}/plugins/designer )
install( TARGETS mydesignerplugin  DESTINATION ${PLUGIN_INSTALL_DIR}/plugins/designer )
</syntaxhighlight>


=== Finding your widget in Qt 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 <tt>MyWidgetDesignerFactory::name()</tt>) in the group "Some group (KDE)" (as defined by <tt>MyWidgetDesignerFactory::group()</tt>). Now drag and drop the entry to the currently edited view in Qt Designer, and voilà, your widget should be added to that view.
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 <tt>MyWidgetDesignerFactory::name()</tt>) in the group "Some group (KDE)" (as defined by <tt>MyWidgetDesignerFactory::group()</tt>). Now drag and drop the entry to the currently edited view in Qt Designer, and voilà, your widget should be added to that view.




== Creating a bundle plugin for several widgets ==
== Creating a Bundle Plugin for several Widgets ==
 
You may have created not just one, but several widgets which you would like to enable support for in Qt Designer. Instead of writing a plugin for each widget you can collect the factories for all widgets in a single plugin, by using a helper class.
 
In the next subsections it is assumed you have already written factories for your two widgets <tt>MyWidget</tt> and <tt>MyOtherWidget</tt> as described in the previous section, namely <tt>MyWidgetDesignerFactory</tt> and <tt>MyOtherWidgetDesignerFactory</tt>, using the headers "mywidgetdesignerfactory.h" and "myotherwidgetdesignerfactory.h" and the implementation files "mywidgetdesignerfactory.cpp" and "myotherwidgetdesignerfactory.cpp".
 
=== The Interface to the Bundle ===
 
The helper class, which is used to hold all factories, needs to be subclassed from {{qt|QObject}} and to implement the interface {{qt|QDesignerCustomWidgetCollectionInterface}}, as in the following example MyWidgetDesignerFactoryCollection in the file "mywidgetdesignerfactorycollection.h":
 
<syntaxhighlight lang="cpp-qt" line="">
#ifndef MYWIDGETDESIGNERFACTORYCOLLECTION_H
#define MYWIDGETDESIGNERFACTORYCOLLECTION_H
 
// Qt
#include <QtDesigner/QDesignerCustomWidgetCollectionInterface>
#include <QtCore/QObject>
 
class MyWidgetDesignerFactoryCollection : public QObject, public QDesignerCustomWidgetCollectionInterface
{
  Q_OBJECT
  Q_INTERFACES( QDesignerCustomWidgetCollectionInterface
 
  public:
    explicit MyWidgetDesignerFactoryCollection( QObject* parent = 0 );
 
  public: // QDesignerCustomWidgetCollectionInterface API
    virtual QList<QDesignerCustomWidgetInterface*> customWidgets() const;
 
  private:
    QList<QDesignerCustomWidgetInterface*> mWidgetFactories;
};
 
#endif
</syntaxhighlight>
 
 
=== Putting the Factories into the Collection  ===
 
The implementation of the helper class is pretty simple. You just have to create all widget factories in the constructor and add them to the list member. This list is then returned in the method <tt>MyWidgetDesignerFactoryCollection::customWidgets()</tt>. See the example code of the file "mywidgetdesignerfactorycollection.cpp":
 
<syntaxhighlight lang="cpp-qt" line="">
#include "mywidgetdesignerfactorycollection.h"
 
// plugin
#include "mywidgetdesignerfactory.h"
#include "myotherwidgetdesignerfactory.h"
// Qt
#include <QtCore/QtPlugin>
 
MyWidgetDesignerFactoryCollection::MyWidgetDesignerFactoryCollection( QObject* parent )
  : QObject( parent )
{
    mWidgetFactories.append( new MyWidgetDesignerFactory(this) );
    mWidgetFactories.append( new MyOtherWidgetDesignerFactory(this) );
}
 
QList<QDesignerCustomWidgetInterface*> MyWidgetDesignerFactoryCollection::customWidgets() const
{
    return mWidgetFactories;
}
 
Q_EXPORT_PLUGIN2( mydesignerplugin, MyWidgetDesignerFactoryCollection )
</syntaxhighlight>
 
Important: As the <tt>MyWidgetDesignerFactoryCollection</tt> is now the central class of the plugin and not any of the widget factories, the source files of the widget factories should no more contain the macro <tt>Q_EXPORT_PLUGIN2</tt>. Otherwise the build will fail.
 
=== Adapting the Buildsystem ===
 
The CMakeLists.txt content is similar to the one for the single widget factory plugin. The two differences are in the files listed for <tt>mydesignerplugin_SRCS</tt>, here you put all the source files of the factories and the one of the helper class, and the libraries listed in <tt>target_link_libraries( ... )</tt>, here you collect all libraries as needed by the single widget factories:
 
<syntaxhighlight lang="cmake">
set( mydesignerplugin_SRCS
  mywidgetdesignerfactory.cpp
  myotherwidgetdesignerfactory.cpp
  # and any other factory source file
  mywidgetdesignerfactorycollection.cpp
)
 
# the name of the plugin module is the same name as used in the macro Q_EXPORT_PLUGIN2 in the file mywidgetdesignerfactorycollection.cpp
kde4_add_plugin( mydesignerplugin  ${mydesignerplugin_SRCS} )
 
target_link_libraries( mydesignerplugin
  mylib
  # other needed libs
)
 
install( TARGETS mydesignerplugin  DESTINATION ${PLUGIN_INSTALL_DIR}/plugins/designer )
</syntaxhighlight>
 
Now if you compile and install this new bundle plugin, after the (re-)start of Qt Designer you should find all your widgets in the Widgetbox. Make sure you have removed any previously installed plugins with a single widget factory for any of the widgets before to avoid conflicts.

Latest revision as of 17:51, 10 August 2024

Proposed for Deletion

This page has been proposed for deletion for the following reason:

Uses the website qt.nokia.com. The page https://community.kde.org/Get_Involved/development/Tutorials and its children should not exist.

Please use the discussion section of this page to voice your opinion on this.

Warning

This page needs a review and probably holds information that needs to be fixed.

Parts to be reviewed:

{{{1}}}



Abstract

This tutorial shows how to add support for custom GUI elements to Qt Designer. In the first section you learn how to write a Qt Designer plugin for a simple custom widget. The second section then presents how to support more than one widget in a plugin.

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 file "mywidgetdesignerfactory.h":

#ifndef MYWIDGETDESIGNERFACTORY_H
#define MYWIDGETDESIGNERFACTORY_H

// Qt
#include <QtDesigner/QDesignerCustomWidgetInterface>
#include <QtCore/QObject>

class MyWidgetDesignerFactory : public QObject, public QDesignerCustomWidgetInterface
{
  Q_OBJECT
  Q_INTERFACES( QDesignerCustomWidgetInterface )

  public:
    explicit MyWidgetDesignerFactory( 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;
};

#endif

Creating the Data as needed by the Interface

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

#include "mywidgetdesignerfactory.h"

// my lib
#include <mywidget.h>
// Qt
#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 file mywidgetdesignerfactory.cpp
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.


Creating a Bundle Plugin for several Widgets

You may have created not just one, but several widgets which you would like to enable support for in Qt Designer. Instead of writing a plugin for each widget you can collect the factories for all widgets in a single plugin, by using a helper class.

In the next subsections it is assumed you have already written factories for your two widgets MyWidget and MyOtherWidget as described in the previous section, namely MyWidgetDesignerFactory and MyOtherWidgetDesignerFactory, using the headers "mywidgetdesignerfactory.h" and "myotherwidgetdesignerfactory.h" and the implementation files "mywidgetdesignerfactory.cpp" and "myotherwidgetdesignerfactory.cpp".

The Interface to the Bundle

The helper class, which is used to hold all factories, needs to be subclassed from QObject and to implement the interface QDesignerCustomWidgetCollectionInterface, as in the following example MyWidgetDesignerFactoryCollection in the file "mywidgetdesignerfactorycollection.h":

#ifndef MYWIDGETDESIGNERFACTORYCOLLECTION_H
#define MYWIDGETDESIGNERFACTORYCOLLECTION_H

// Qt
#include <QtDesigner/QDesignerCustomWidgetCollectionInterface>
#include <QtCore/QObject>

class MyWidgetDesignerFactoryCollection : public QObject, public QDesignerCustomWidgetCollectionInterface
{
  Q_OBJECT
  Q_INTERFACES( QDesignerCustomWidgetCollectionInterface

  public:
    explicit MyWidgetDesignerFactoryCollection( QObject* parent = 0 );

  public: // QDesignerCustomWidgetCollectionInterface API
    virtual QList<QDesignerCustomWidgetInterface*> customWidgets() const;

  private:
    QList<QDesignerCustomWidgetInterface*> mWidgetFactories;
};

#endif


Putting the Factories into the Collection

The implementation of the helper class is pretty simple. You just have to create all widget factories in the constructor and add them to the list member. This list is then returned in the method MyWidgetDesignerFactoryCollection::customWidgets(). See the example code of the file "mywidgetdesignerfactorycollection.cpp":

#include "mywidgetdesignerfactorycollection.h"

// plugin
#include "mywidgetdesignerfactory.h"
#include "myotherwidgetdesignerfactory.h"
// Qt
#include <QtCore/QtPlugin>

MyWidgetDesignerFactoryCollection::MyWidgetDesignerFactoryCollection( QObject* parent )
  : QObject( parent )
{
     mWidgetFactories.append( new MyWidgetDesignerFactory(this) );
     mWidgetFactories.append( new MyOtherWidgetDesignerFactory(this) );
}

QList<QDesignerCustomWidgetInterface*> MyWidgetDesignerFactoryCollection::customWidgets() const
{
    return mWidgetFactories;
}

Q_EXPORT_PLUGIN2( mydesignerplugin, MyWidgetDesignerFactoryCollection )

Important: As the MyWidgetDesignerFactoryCollection is now the central class of the plugin and not any of the widget factories, the source files of the widget factories should no more contain the macro Q_EXPORT_PLUGIN2. Otherwise the build will fail.

Adapting the Buildsystem

The CMakeLists.txt content is similar to the one for the single widget factory plugin. The two differences are in the files listed for mydesignerplugin_SRCS, here you put all the source files of the factories and the one of the helper class, and the libraries listed in target_link_libraries( ... ), here you collect all libraries as needed by the single widget factories:

set( mydesignerplugin_SRCS
  mywidgetdesignerfactory.cpp
  myotherwidgetdesignerfactory.cpp
  # and any other factory source file
  mywidgetdesignerfactorycollection.cpp
)

# the name of the plugin module is the same name as used in the macro Q_EXPORT_PLUGIN2 in the file mywidgetdesignerfactorycollection.cpp
kde4_add_plugin( mydesignerplugin  ${mydesignerplugin_SRCS} )

target_link_libraries( mydesignerplugin
  mylib
  # other needed libs
)

install( TARGETS mydesignerplugin  DESTINATION ${PLUGIN_INSTALL_DIR}/plugins/designer )

Now if you compile and install this new bundle plugin, after the (re-)start of Qt Designer you should find all your widgets in the Widgetbox. Make sure you have removed any previously installed plugins with a single widget factory for any of the widgets before to avoid conflicts.