To use custom controls in QT designer, you must make QT designer aware of the existence of custom controls. There are two ways to notify QT designer of the information about the new custom control: the promotion method and the plug-in method. The upgrade method is the easiest and quickest. As the name suggests, the upgrade method is to upgrade qt's own controls. Select a QT custom control. If it has a similar API with the newly added custom control, you only need to complete the information about the new control in the QT designer dialog box, the new control can be used in the form created by QT designer. However, during editing and preview, it is similar to the self-owned QT control. Now insert the hexspinbox control into a form using the Upgrade Method: 1. Use QT designer to create a new form and add the qspinbox in the control box to the form. 2. Right-click spin box and select the context menu "promote to M widget. 3. In the pop-up dialog box, enter "hexspinbox" in the class name and "hexspinbox. H" in the header file. The code generated by UIC contains the "hexspinbox. H" header file instead of the <qspinbox> header file. It also contains an hexspinbox instance instead of the qspinbox. In QT designer, the hexspinbox control is represented by qspinbox, and all attributes (such as the range and current value) of qspinbox can be set ).
The disadvantage of the upgrade method is that you cannot set the properties of custom controls in QT designer or draw your own. These problems can be solved using plug-ins. The plug-in method requires creating a plug-in library so that QT designer can be loaded in real time to create control instances. In this way, QT designer can use custom controls when editing a form or previewing. With qt's meta-Object System, QT designer can dynamically obtain all attributes of custom controls. Taking iconeditor as an example, we use plug-ins to integrate iconeditor into QT designer. First, we will derive a new class from qdesignercustomwidgetinterface and rewrite some virtual functions. Assume that the source code of this plug-in is in the iconeditorplugin directory, and the code of the iconeditor class is in the directory iconeditor that is parallel to it. Here is the definition of the plug-in class:
#include <QDesignerCustomWidgetInterface>class IconEditorPlugin : public QObject, public QDesignerCustomWidgetInterface{ Q_OBJECT Q_INTERFACES(QDesignerCustomWidgetInterface)public: IconEditorPlugin(QObject *parent = 0); QString name() const; QString includeFile() const; QString group() const; QIcon icon() const; QString toolTip() const; QString whatsThis() const; bool isContainer() const; QWidget *createWidget(QWidget *parent);};
The iconeditorplugin class is a factory class that encapsulates the iconeditor control. It uses double inheritance. The parent class is qobject and qdesignercustomwidgetinterface. Macro q_interfaces () tells MOC that the second base class is a plug-in interface class. Qt designer uses the functions in this class to create an instance of iconeditor and obtain information about it. The source file is as follows:
IconEditorPlugin::IconEditorPlugin(QObject *parent) : QObject(parent){}QString IconEditorPlugin::name() const{ return "IconEditor";}QString IconEditorPlugin::includeFile() const{ return "iconeditor.h";}QString IconEditorPlugin::group() const{ return tr("Image Manipulation Widgets");}QIcon IconEditorPlugin::icon() const{ return QIcon(":/images/iconeditor.png");}QString IconEditorPlugin::toolTip() const{ return tr("An icon editor widget");}QString IconEditorPlugin::whatsThis() const{ return tr("This widget is presented in Chapter 5 of <i>C++ GUI " "Programming with Qt 4</i> as an example of a custom Qt " "widget.");}bool IconEditorPlugin::isContainer() const{ return false;}QWidget *IconEditorPlugin::createWidget(QWidget *parent){ return new IconEditor(parent);} Q_EXPORT_PLUGIN2(iconeditorplugin, IconEditorPlugin)
Constructor is an empty function. Function Name () returns the name of the control provided by the plug-in. Function includefile () to obtain the header file of a specific control encapsulated by the plug-in. This header file is included in the Code generated by the UIC tool. The function group () returns the name of the box group to which the custom control belongs. If this name is not in QT designer, a new group is created for this control. Function icon () returns the icon used by the custom control in QT designer. Here we assume that iconeditorplugin has an associated resource file, which contains an interface for the icon editor image. The tooltip () function is completed in the control box of QT designer. When you move the cursor to the custom control, a string is displayed as a prompt. The whatsthis () function returns the question "What's this" displayed by QT designer. If the iscontainer () function returns true, this control can contain other controls. For example, if qframe can contain other controls, it is a container control. Many QT controls can contain other controls. However, if iscontainer () returns false, QT designer does not allow this control to contain other controls. Qt designer calls the createwidget () function to create a control instance of the specified parent control. Macro q_export_plugin2 () must be declared at the end of the source file. This macro enables QT designer to obtain this plug-in. The first parameter is the name of the plug-in, and the second parameter is the name of the plug-in class .. The Pro file is as follows: template = libconfig + = designer plugin releaseheaders = .. /iconeditor. h/iconeditorplugin. hsources = .. /iconeditor. CPP/iconeditorplugin. cppresources = iconeditorplugin. qrcdestdir = $ (qtdir)/plugins/designer. the Pro file assumes that the environment variable qtdir is located in the QT installation directory. After running make or nmake to compile the plug-in, the program automatically installs the plug-in to the Plugins directory of QT designer. After the installation is successful, we can use the iconeditor control in QT designer like other controls.
To integrate multiple custom controls in QT designer, you can create a plug-in for each control, or combine all the controls into a single plug-in, derived from qdesignercustomwidgetcollectioninterface.
Address: http://blog.csdn.net/Last_Impression/article/details/2438881