<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- /home/gvatteka/dev/qt-4.3/doc/src/designer-manual.qdoc --> <head> <title>Creating Custom Widget Extensions</title> <link href="classic.css" rel="stylesheet" type="text/css" /> </head> <body> <h1 align="center">Creating Custom Widget Extensions<br /><small></small></h1> <p>Once you have a custom widget plugin for <i>Qt Designer</i>, you can provide it with the expected behavior and functionality within <i>Qt Designer</i>'s workspace, using custom widget extensions.</p> <ul><li><a href="#extension-types">Extension Types</a></li> <li><a href="#creating-an-extension">Creating an Extension</a></li> <li><a href="#exposing-an-extension-to-qt-designer">Exposing an Extension to Qt Designer</a></li> <ul><li><a href="#creating-an-extension-factory">Creating an Extension Factory</a></li> <li><a href="#accessing-qt-designer-s-extension-manager">Accessing Qt Designer's Extension Manager</a></li> </ul> <li><a href="#related-examples">Related Examples</a></li> </ul> <a name="extension-types"></a> <h2>Extension Types</h2> <p>There are several available types of extensions in <i>Qt Designer</i>. You can use all the extensions following the same pattern, only replacing the respective extension base class.</p> <p>QDesignerTaskMenuExtension is useful for custom widgets while QDesignerContainerExtension is necessary when implementing a custom multi-page container.</p> <p><table width="100%" align="center" cellpadding="2" cellspacing="1" border="0"> <tr valign="top" class="odd"><td><img src="images/designer-manual-taskmenuextension.png" /></td><td><b>QDesignerTaskMenuExtension</b><p>QDesignerTaskMenuExtension provides an extension that allows you to add custom menu entries to <i>Qt Designer</i>'s task menu.</p> <p>See also the Task Menu Extension</tt> example.</p> </td></tr> <tr valign="top" class="even"><td><img src="images/designer-manual-containerextension.png" /></td><td><b>QDesignerContainerExtension</b><p>QDesignerContainerExtension provides an extension that allows you to add (and delete) pages to a multi-page container plugin in <i>Qt Designer</i>.</p> <p>See also the Container Extension</tt> example.</p> <p><b>Note:</b> It is not possible to add custom per-page properties to certain widgets (e.g., <a href="gui/QTabWidget.html"><tt>QTabWidget</tt></a>) due to the way they are implemented. This limitation will be addressed in future versions of Qt.</p> </td></tr> </table></p> <p>The usage of <a href="%2E%2E/tools/designer/QDesignerMemberSheetExtension.html"><tt>QDesignerMemberSheetExtension</tt></a> and <a href="%2E%2E/tools/designer/QDesignerPropertySheetExtension.html"><tt>QDesignerPropertySheetExtension</tt></a> is more rare, but the classes enables you to manipulate the appearance of class members within <i>Qt Designer</i>'s workspace.</p> <p><table width="100 %" align="center" cellpadding="2" cellspacing="1" border="0"> <tr valign="top" class="odd"><td><img src="images/designer-manual-membersheetextension.png" /></td><td><b><a href="%2E%2E/tools/designer/QDesignerMemberSheetExtension.html"><tt>QDesignerMemberSheetExtension</tt></a></b><p>The <a href="%2E%2E/tools/designer/QDesignerMemberSheetExtension.html"><tt>QDesignerMemberSheetExtension</tt></a> class allows you to manipulate a widget's member functions displayed when connecting signals and slots.</p> </td></tr> <tr valign="top" class="even"><td><img src="images/designer-manual-propertysheetextension.png" /></td><td><b><a href="%2E%2E/tools/designer/QDesignerPropertySheetExtension.html"><tt>QDesignerPropertySheetExtension</tt></a>, QDesignerDynamicPropertySheetExtension</b><p>These extension classes allow you to manipulate a widget's properties displayed in <i>Qt Designer</i>'s property editor.</p> </td></tr> <tr valign="top" class="odd"><td></td><td><b>QDesignerScriptExtension</b><p>The QDesignerScriptExtension class allows you to define script snippets that are executed when a form is loaded. The extension is primarily intended to be used to set up the internal states of custom widgets.</p> </td></tr> </table></p> <p><i>Qt Designer</i> uses the <a href="%2E%2E/tools/designer/QDesignerPropertySheetExtension.html"><tt>QDesignerPropertySheetExtension</tt></a> and the <a href="%2E%2E/tools/designer/QDesignerMemberSheetExtension.html"><tt>QDesignerMemberSheetExtension</tt></a> classes to feed its property and signal and slot editors. Whenever a widget is selected in its workspace, <i>Qt Designer</i> will query for the widget's property sheet extension, and whenever a connection between two widgets is requested, <i>Qt Designer</i> will query for the widgets' member sheet extensions.</p> <p><b>Warning:</b> All widgets have default property and member sheets. But if you implement custom property sheet or member sheet extensions, these extensions will override the default sheets.</p> <a name="creating-an-extension"></a> <h2>Creating an Extension</h2> <p>To create an extension you must inherit both <a href="core/QObject.html"><tt>QObject</tt></a> and the appropriate base class, and reimplement its functions. Since we are implementing an interface, we must ensure that it's made known to the meta object system using the Q_INTERFACES() macro in the extension class's definition. For example:</p> <pre> class MyExtension: public QObject, public QdesignerContainerExtension { Q_OBJECT Q_INTERFACE(QDesignerContainerExtension) ... }</pre> <p>This enables <i>Qt Designer</i> to use the qobject_cast() function to query for supported interfaces using nothing but a <a href="core/QObject.html"><tt>QObject</tt></a> pointer.</p> <a name="exposing-an-extension-to-qt-designer"></a> <h2>Exposing an Extension to Qt Designer</h2> <p>In <i>Qt Designer</i> the extensions are not created until they are required. For that reason, when implementing extensions, you must subclass QExtensionFactory to create a class that is able to make instances of your extensions. In addition you must register your factory with <i>Qt Designer</i>'s extension manager; the extension manager controls the construction of extensions as they are required.</p> <p>When an extension is requested, <i>Qt Designer</i>'s extension manager will run through all its registered factories calling QExtensionFactory::createExtension() for each until it finds one that is able to create the requested extension for the selected widget. This factory will then make an instance of the extension.</p> <p align="center"><img src="images/qtdesignerextensions.png" /></p><a name="creating-an-extension-factory"></a> <h3>Creating an Extension Factory</h3> <p>The QExtensionFactory class provides a standard extension factory, but can also be used as an interface for custom extension factories. The purpose is to reimplement the QExtensionFactory::createExtension() function, making it able to create your extension, such as a MultiPageWidget container extension.</p> <p>You can either create a new QExtensionFactory and reimplement the QExtensionFactory::createExtension() function:</p> <pre> QObject *ANewExtensionFactory::createExtension(QObject *object, const QString &iid, QObject *parent) const { if (iid != Q_TYPEID(QDesignerContainerExtension)) return 0; if (MyCustomWidget *widget = qobject_cast<MyCustomWidget*> (object)) return new MyContainerExtension(widget, parent); return 0; }</pre> <p>or you can use an existing factory, expanding the QExtensionFactory::createExtension() function to enable the factory to create your custom extension as well:</p> <pre> QObject *AGeneralExtensionFactory::createExtension(QObject *object, const QString &iid, QObject *parent) const { MyCustomWidget *widget = qobject_cast<MyCustomWidget*>(object); if (widget && (iid == Q_TYPEID(QDesignerTaskMenuExtension))) { return new MyTaskMenuExtension(widget, parent); } else if (widget && (iid == Q_TYPEID(QDesignerContainerExtension))) { return new MyContainerExtension(widget, parent); } else { return 0; } }</pre> <a name="accessing-qt-designer-s-extension-manager"></a> <h3>Accessing Qt Designer's Extension Manager</h3> <p>When implementing a custom widget plugin, you must subclass the QDesignerCustomWidgetInterface to expose your plugin to Qt Designer. This is covered in more detail in the <a href="designer-creating-custom-widgets.html">Creating Custom Widgets for Qt Designer</tt></a> section. The registration of an extension factory is typically made in the QDesignerCustomWidgetInterface::initialize() function:</p> <pre> void MyPlugin::initialize(QDesignerFormEditorInterface *formEditor) { if (initialized) return; QExtensionManager *manager = formEditor->extensionManager(); Q_ASSERT(manager != 0); manager->registerExtensions(new MyExtensionFactory(manager), Q_TYPEID(QDesignerTaskMenuExtension)); initialized = true; }</pre> <p>The <tt>formEditor</tt> parameter in the QDesignerCustomWidgetInterface::initialize() function is a pointer to <i>Qt Designer</i>'s current QDesignerFormEditorInterface object. You must use the QDesignerFormEditorInterface::extensionManager() function to retrieve an interface to <i>Qt Designer</i>'s extension manager. Then you use the QExtensionManager::registerExtensions() function to register your custom extension factory.</p> <a name="related-examples"></a> <h2>Related Examples</h2> <p>Please see the Task Menu Extension</tt> and Container Extension</tt> examples for more information about creating custom widget extensions in <i>Qt Designer</i>.</p> <p /><address><hr /><div align="center"> <table width="100%" cellspacing="0" border="0"><tr class="address"> <td width="30%">Copyright © 2007 <a href="trolltech.html">Trolltech</a></td> <td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td> <td width="30%" align="right"><div align="right">Qt Jambi </div></td> </tr></table></div></address></body> </html>