How custom plugins are loaded into QT designer (verbose)

Source: Internet
Author: User
Tags qt designer

To use custom controls in Qt Designer, you must make QT Designer aware of the existence of our custom controls. There are two ways to notify Qt Designer of the new custom control's information: the "Upgrade (Promotion)" method and the plug-in method.
The upgrade method is the most convenient and quick. As the name implies, the upgrade method is to upgrade the QT-owned control to retrofit. Choose a QT-owned control, if it has a similar API to our new custom control, then just complete the information about the new control in Qt Designer's dialog box, and the new control can be used in the form created by QT Designer. However, when editing and previewing, it is no different from Qt's own control.
Now insert the Hexspinbox control into a form using the upgrade method:
1. Create a new form with QT Designer and add the Qspinbox in the control box to the form.
2. Right-click spin box and select the "Promote to Custom Widget" context menu.
3. In the popup dialog box, the class name is filled with "Hexspinbox", the header file "Hexspinbox.h"
All right. The code generated by UIC contains the "hexspinbox.h" header file instead of the <qspinbox&gt, and it also contains an instance of Hexspinbox, not qspinbox. In Qt Designer, the Hexspinbox control is represented by Qspinbox, and all properties of the Qspinbox (such as range, current value) can be set.
The disadvantage of the upgrade method is that it is not possible to set the custom control's own properties in Qt Designer or to draw itself. These problems can be solved by plug-in method.
The plug-in method needs to create a plug-in library that allows QT designer to load in real time to create an instance of the control. This allows Qt designer to use a custom control when editing a form or preview. With Qt's meta-object system, QT Designer can dynamically get all the properties of a custom control. Now take IconEditor as an example, using the plug-in method to integrate IconEditor into QT designer.
First, we derive a new class from Qdesignercustomwidgetinterface and rewrite some virtual functions. We assume that the source code for this plug-in is in the Iconeditorplugin directory, and that the IconEditor class is in a directory iconeditor 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, which uses double inheritance, and 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 to get information about it.
The source files are 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 was 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)


The constructor is an empty function.
The function name () returns the name of the control provided by the plug-in.
The function includefile () Gets the header file of the specific control encapsulated by the plug-in, which is contained in the code generated by the UIC tool.
The function group () returns the name of the Toolbox (box Group) to which the custom control belongs. If this name is not in QT Designer, a new group will be created for the control.
function icon () returns the icon that the custom control uses in Qt Designer. Here we assume that Iconeditorplugin has an associated resource file that has an interface to an icon editor image.
The function tooltip () is completed in the Qt Designer control box, and the display string is displayed as a hint when the mouse moves to the custom control.
The function Whatsthis () returns the QT designer display "What's this" question.
The function Iscontainer () returns true to indicate that the control can contain other controls. For example, Qframe can contain other controls, it is a container control. Many Qt controls can contain other controls, but if Iscontainer () returns FALSE,QT designer, the control is not allowed to contain other controls.
The Qt Designer call Function Createwidget () Creates a control instance that specifies the parent control.
The macro q_export_plugin2 () must be declared at the end of the source file, which allows QT designer to get the plugin. The first parameter is the name of the plugin, and the second parameter is the name of the plugin class that implements it.
The. Pro files are as follows:
TEMPLATE = Lib
CONFIG + = Designer Plugin Release
HEADERS =.. /iconeditor/iconeditor.h \
Iconeditorplugin.h
SOURCES =.. /iconeditor/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 compiling the plug-in, the program automatically installs it into the QT designer's plugins directory. Once the installation is successful, we can use the IconEditor control in QT Designer just like any other control.
If you want to integrate multiple custom controls in Qt Designer, you can create one plug-in for each control, or you can combine all the controls into a single plug-in, derived from Qdesignercustomwidgetcollectioninterface.
Originally this is from "C + + GUI programming with Qt 4" translation come over. Read this part of the book, mentioned 2 methods, so try the plug-in method, after a try, install a new plugin to QT Designer, the effect is good. (episode: Plugin file should also contain <qtgui>, otherwise GCC does not know Qexport_plugin2 ())

How custom plugins are loaded into QT designer (verbose)

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.