![]() |
Home · Overviews · Examples |
Once you have a custom widget plugin for Qt Designer, you can provide it with the expected behavior and functionality within Qt Designer's workspace, using custom widget extensions.
QDesignerTaskMenuExtension is useful for custom widgets while QDesignerContainerExtension is necessary when implementing a custom multi-page container.
![]() | QDesignerTaskMenuExtension QDesignerTaskMenuExtension provides an extension that allows you to add custom menu entries to Qt Designer's task menu. See also the Task Menu Extension example. |
![]() | QDesignerContainerExtension QDesignerContainerExtension provides an extension that allows you to add (and delete) pages to a multi-page container plugin in Qt Designer. See also the Container Extension example. Note: It is not possible to add custom per-page properties to certain widgets (e.g., QTabWidget) due to the way they are implemented. This limitation will be addressed in future versions of Qt. |
![]() | QDesignerMemberSheetExtension The QDesignerMemberSheetExtension class allows you to manipulate a widget's member functions displayed when connecting signals and slots. |
![]() | QDesignerPropertySheetExtension, QDesignerDynamicPropertySheetExtension These extension classes allow you to manipulate a widget's properties displayed in Qt Designer's property editor. |
QDesignerScriptExtension 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. |
Warning: 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.Creating an Extension
To create an extension you must inherit both QObject 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:
class MyExtension: public QObject, public QdesignerContainerExtension { Q_OBJECT Q_INTERFACE(QDesignerContainerExtension) ... }This enables Qt Designer to use the qobject_cast() function to query for supported interfaces using nothing but a QObject pointer.
When an extension is requested, Qt Designer'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.
You can either create a new QExtensionFactory and reimplement the QExtensionFactory::createExtension() function:
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; }or you can use an existing factory, expanding the QExtensionFactory::createExtension() function to enable the factory to create your custom extension as well:
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; } }
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; }The formEditor parameter in the QDesignerCustomWidgetInterface::initialize() function is a pointer to Qt Designer's current QDesignerFormEditorInterface object. You must use the QDesignerFormEditorInterface::extensionManager() function to retrieve an interface to Qt Designer's extension manager. Then you use the QExtensionManager::registerExtensions() function to register your custom extension factory.
Copyright © 2008 Trolltech | Trademarks | Qt Jambi 4.4.0_01 |