How to load custom plug-ins to QT designer

Source: Internet
Author: User
Tags qt designer

 

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 fill in "hexspinbox. H" in the header file"
Okay. 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 = lib
Config + = designer plugin release
Headers = ../iconeditor. h/
Iconeditorplugin. h
Sources = ../iconeditor. CPP/
Iconeditorplugin. cpp
Resources = iconeditorplugin. qrc
Destdir = $ (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.
This was originally translated from "C ++ GUI programming with QT 4. I read this part of the book and mentioned two methods. So I tried the plug-in method. After some attempts, I installed the new plug-in to QT designer. The effect was good.

To delete a plug-in after installing the plug-in, you only need to delete the corresponding. dll and. Lib files under the/plugins file.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.