<?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"> <!-- designer-manual.qdoc --> <head> <title>Qt 4.6: Creating Custom Widget Extensions</title> <link rel="prev" href="designer-creating-custom-widgets.html" /> <link rel="contents" href="designer-manual.html" /> <link rel="next" href="designer-ui-file-format.html" /> <link href="classic.css" rel="stylesheet" type="text/css" /> </head> <body> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td> <td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="functions.html"><font color="#004faf">All Functions</font></a> · <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><p> [Previous: <a href="designer-creating-custom-widgets.html">Creating Custom Widgets for Qt Designer</a>] [<a href="designer-manual.html">Contents</a>] [Next: <a href="designer-ui-file-format.html">Qt Designer's UI File Format</a>] </p> <h1 class="title">Creating Custom Widget Extensions<br /><span class="subtitle"></span> </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> <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 of these extensions in the same pattern, only replacing the respective extension base class.</p> <p><a href="qdesignercontainerextension.html">QDesignerContainerExtension</a> is necessary when implementing a custom multi-page container.</p> <p><table class="generic" align="center" cellpadding="2" cellspacing="1" border="0"> <tr valign="top" class="odd"><td><img src="images/designer-manual-taskmenuextension.png" /></td><td><b><a href="qdesignertaskmenuextension.html">QDesignerTaskMenuExtension</a></b><p><a href="qdesignertaskmenuextension.html">QDesignerTaskMenuExtension</a> is useful for custom widgets. It provides an extension that allows you to add custom menu entries to <i>Qt Designer</i>'s task menu.</p> <p>The <a href="designer-taskmenuextension.html">Task Menu Extension</a> example illustrates how to use this class.</p> </td></tr> <tr valign="top" class="even"><td><img src="images/designer-manual-containerextension.png" /></td><td><b><a href="qdesignercontainerextension.html">QDesignerContainerExtension</a></b><p><a href="qdesignercontainerextension.html">QDesignerContainerExtension</a> is necessary when implementing a custom multi-page container. It provides an extension that allows you to add and delete pages for a multi-page container plugin in <i>Qt Designer</i>.</p> <p>The <a href="designer-containerextension.html">Container Extension</a> example further explains how to use this class.</p> <p><b>Note:</b> It is not possible to add custom per-page properties for some widgets (e.g., <a href="qtabwidget.html">QTabWidget</a>) due to the way they are implemented.</p> </td></tr> </table></p> <p><table class="generic" 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="qdesignermembersheetextension.html">QDesignerMemberSheetExtension</a></b><p>The <a href="qdesignermembersheetextension.html">QDesignerMemberSheetExtension</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="qdesignerpropertysheetextension.html">QDesignerPropertySheetExtension</a>, <a href="qdesignerdynamicpropertysheetextension.html">QDesignerDynamicPropertySheetExtension</a></b><p>These extension classes allow you to control how a widget's properties are displayed in <i>Qt Designer</i>'s property editor.</p> </td></tr> </table></p> <p><i>Qt Designer</i> uses the <a href="qdesignerpropertysheetextension.html">QDesignerPropertySheetExtension</a> and the <a href="qdesignermembersheetextension.html">QDesignerMemberSheetExtension</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; likewise, 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. If you implement custom property sheet or member sheet extensions, your custom 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="qobject.html">QObject</a> and the appropriate base class, and reimplement its functions. Since we are implementing an interface, we must ensure that it is made known to the meta object system using the <a href="qobject.html#Q_INTERFACES">Q_INTERFACES</a>() 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 <a href="qobject.html#qobject_cast">qobject_cast</a>() function to query for supported interfaces using a <a href="qobject.html">QObject</a> pointer only.</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 this reason, when implementing extensions, you must subclass <a href="qextensionfactory.html">QExtensionFactory</a> to create a class that is able to make instances of your extensions. Also, you must register your factory with <i>Qt Designer</i>'s extension manager; the extension manager handles the construction of extensions.</p> <p>When an extension is requested, <i>Qt Designer</i>'s extension manager will run through its registered factories calling <a href="qextensionfactory.html#createExtension">QExtensionFactory::createExtension</a>() for each of them 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 <a href="qextensionfactory.html">QExtensionFactory</a> class provides a standard extension factory, but it can also be used as an interface for custom extension factories.</p> <p>The purpose is to reimplement the <a href="qextensionfactory.html#createExtension">QExtensionFactory::createExtension</a>() function, making it able to create your extension, such as a <a href="designer-containerextension.html">MultiPageWidget</a> container extension.</p> <p>You can either create a new <a href="qextensionfactory.html">QExtensionFactory</a> and reimplement the <a href="qextensionfactory.html#createExtension">QExtensionFactory::createExtension</a>() 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 <a href="qextensionfactory.html#createExtension">QExtensionFactory::createExtension</a>() 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 <a href="qdesignercustomwidgetinterface.html">QDesignerCustomWidgetInterface</a> to expose your plugin to <i>Qt Designer</i>. This is covered in more detail in the <a href="designer-creating-custom-widgets.html">Creating Custom Widgets for Qt Designer</a> section. The registration of an extension factory is typically made in the <a href="qdesignercustomwidgetinterface.html#initialize">QDesignerCustomWidgetInterface::initialize</a>() 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 <a href="qdesignercustomwidgetinterface.html#initialize">QDesignerCustomWidgetInterface::initialize</a>() function is a pointer to <i>Qt Designer</i>'s current <a href="qdesignerformeditorinterface.html">QDesignerFormEditorInterface</a> object. You must use the <a href="qdesignerformeditorinterface.html#extensionManager">QDesignerFormEditorInterface::extensionManager</a>() function to retrieve an interface to <i>Qt Designer</i>'s extension manager. Then you use the <a href="qextensionmanager.html#registerExtensions">QExtensionManager::registerExtensions</a>() function to register your custom extension factory.</p> <a name="related-examples"></a> <h2>Related Examples</h2> <p>For more information on creating custom widget extensions in <i>Qt Designer</i>, refer to the <a href="designer-taskmenuextension.html">Task Menu Extension</a> and <a href="designer-containerextension.html">Container Extension</a> examples.</p> <p> [Previous: <a href="designer-creating-custom-widgets.html">Creating Custom Widgets for Qt Designer</a>] [<a href="designer-manual.html">Contents</a>] [Next: <a href="designer-ui-file-format.html">Qt Designer's UI File Format</a>] </p> <p /><address><hr /><div align="center"> <table width="100%" cellspacing="0" border="0"><tr class="address"> <td width="40%" align="left">Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies)</td> <td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td> <td width="40%" align="right"><div align="right">Qt 4.6.3</div></td> </tr></table></div></address></body> </html>