Qt DesignerIt provides programmers with visual design capabilities and can be used to develop all or part of forms in an application. To put it simply, cross-compilation is to generate executable code on one platform. The so-called platform actually contains two concepts: Architecture) and Operating System (Operating System ). The same architecture can run different operating systems. Likewise, the same operating system can run on different architectures.
Integrate custom controls into Qt Designer
ToQt DesignerTo use a custom control, you mustQt DesignerCan know ourCustom Controls. There are two ways to change the newCustom ControlsInformation notificationQt Designer: "Upgrade" method and 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. Find a self-contained Qt control.Custom ControlsIf there are similar APIsQt DesignerIn the dialog box to complete some newWidgetThe new control can beQt Designer. However, during editing and previewing, it is no different from the original Qt control.
Now integrate the HexSpinBox control with the upgrade methodQt DesignerMedium:
1. Use Qt Designer to create a new form and add the QSpinBox in the control box to the form.
2. Right-click the rotating 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. In the control file generated by uic that contains a QSpinBox, the file contains changes to "hexspinbox. h" and is initialized to an HexSpinBox instance instead of QSpinBox. In Qt Designer, the control expressed by QSpinBox is HexSpinBox, and all attributes of QSpinBox can be set.
Figure 5.6. Qt Designer's custom widget dialog
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 dynamic library so that Qt Designer can load and create control instances in real time. In this way, Qt Designer can use custom controls when editing a form or previewing. Qt Designer uses Qt's meta-object System to dynamically obtain all attributes of the custom control. Taking IconEditor as an example, we use plug-ins to integrate IconEditor into Qt Designer.
First, we inherit a class from QDesignerCustomWidgetInterface and rewrite some virtual functions. Let's assume that the source code of this class 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);
- };
IconEditorPlugin is a class factory that encapsulates the IconEditor control and 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 functions in the 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 control name.
Function includeFile () to obtain the control header file, which is included in the Code generated by moc.
The function group () returns the name of the toolbox to which the 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 Control in Qt Designer. Here we assume that IconEditorPlugin has an associated resource file, which contains an image of the icon editor.
In the control box of Qt Designer, move the cursorCustom ControlsThe string returned by toolTip () 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, but if isContainer () returns false, QtDesignerThis control is not allowed to contain other controls.
Qt DesignerCreateWidget () Creation FunctionWidgetInstance, specifying the parentWidget.
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/iconeditor.h \
- iconeditorplugin.h
- SOURCES = ../iconeditor/iconeditor.cpp \
- iconeditorplugin.cpp
- RESOURCES = iconeditorplugin.qrc
- DESTDIR = $(QTDIR)/plugins/designer
The. pro file assumes that QTDIR is locatedQtInstallation directory. After running make or nmake, the program automatically installsQt Designer. After the installation is successful, we canWidgetSame inQt Designer. If you wantQt DesignerIntegrate multipleWidgetFor eachWidgetYou can also use QDesignerCustomWidgetCollectionInterface to create a plug-in library with the token installed above.