Integrate custom parts created by Qt into Qt Designer

Source: Internet
Author: User
Tags qt designer

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:

 
 
  1. #include <QDesignerCustomWidgetInterface> 
  2.  
  3. class IconEditorPlugin : public QObject, public QDesignerCustomWidgetInterface  
  4. {  
  5.     Q_OBJECT  
  6.     Q_INTERFACES(QDesignerCustomWidgetInterface)  
  7. public:  
  8.     IconEditorPlugin(QObject *parent = 0);  
  9.     QString name() const;  
  10.     QString includeFile() const;  
  11.     QString group() const;  
  12.     QIcon icon() const;  
  13.     QString toolTip() const;  
  14.     QString whatsThis() const;  
  15.     bool isContainer() const;  
  16.     QWidget *createWidget(QWidget *parent);  
  17. }; 

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:

 
 
  1. IconEditorPlugin::IconEditorPlugin(QObject *parent)  
  2.     : QObject(parent)  
  3. {  
  4. }  
  5. QString IconEditorPlugin::name() const  
  6. {  
  7.     return "IconEditor";  
  8. }  
  9. QString IconEditorPlugin::includeFile() const  
  10. {  
  11.     return "iconeditor.h";  
  12. }  
  13. QString IconEditorPlugin::group() const  
  14. {  
  15.     return tr("Image Manipulation Widgets");  
  16. }  
  17. QIcon IconEditorPlugin::icon() const  
  18. {  
  19.     return QIcon(":/images/iconeditor.png");  
  20. }  
  21. QString IconEditorPlugin::toolTip() const  
  22. {  
  23.     return tr("An icon editor widget");  
  24. }  
  25. QString IconEditorPlugin::whatsThis() const  
  26. {  
  27.     return tr("This widget is presented in Chapter 5 of <i>C++ GUI "  
  28.               "Programming with Qt 4</i> as an example of a custom Qt "  
  29.               "widget.");  
  30. }  
  31. bool IconEditorPlugin::isContainer() const  
  32. {  
  33.     return false;  
  34. }  
  35. QWidget *IconEditorPlugin::createWidget(QWidget *parent)  
  36. {  
  37.     return new IconEditor(parent);  
  38. }   
  39. 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:

 
 
  1. TEMPLATE        = lib   
  2. CONFIG         += designer plugin release   
  3. HEADERS         = ../iconeditor/iconeditor.h \   
  4.                   iconeditorplugin.h   
  5. SOURCES         = ../iconeditor/iconeditor.cpp \   
  6.                   iconeditorplugin.cpp   
  7. RESOURCES       = iconeditorplugin.qrc   
  8. 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.

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.