Qt custom plugin

Reference blog: https://blog.csdn.net/giselite/article/details/12622429
Plug-in introduction:
    Qt Designer's plug-in-based architecture allows user-defined and third-party custom widgets to be edited, just like standard Qt controls. All custom widget functions are provided to Qt Designer, including widget properties, signals and slots. Since Qt Designer uses real parts during the design process, the custom parts will be the same as those in the design phase during preview.
 
 
    As shown in the figure, Designer includes custom plug-ins (Clock, Qled) and third-party plug-ins (QWT). Okay, let's take a look at the effect first. Later, there will be specific explanations on how to achieve it. Take your time. . .
 
    To integrate custom widgets for Qt Designer, you need a suitable widget and appropriate .pro file.
 
Provides an interface description
    In order to inform Qt Designer that you want to provide the type of widget, you need to create a subclass of QDesignerCustomWidgetInterface , which describes the various attributes exposed by the widget, most of which are provided by pure virtual functions in the base class, because only the plug-in The author can provide this information.

serial number

function

Return value description

1

name()

Provides the class name of the plugin

2

group()

The widget box of Qt Designer in the group to which the control belongs

3

toolTip()

A short description to help users identify the components in Qt Designer

4

whatsThis ()

A longer description of components designed for Qt Designer users

5

includeFile()

The header file must be included in the application that uses the plug-in. This information is stored in the UI file, and appropriate #includes statements will be created by UIC to include code in the form of custom plug-ins.

6

icon()

The icon of the small window in the plug-in box of Qt Designer

7

isContainer()

true means the component will be used to save the sub-component, otherwise it is false

8

 

 

 

9

 

 

10

createWidget()

 

 

domXml ()

 

 

codeTemplate()

An instance of a QWidget pointer to a custom widget, constructing the provided parent.

Note: createWidget() is a factory method, which is only responsible for the function of creating widgets. The properties of the custom widget will not be available until load() returns.

 

Describes the attributes of the widget, such as object name, size hint, and other standard QWidget attribute descriptions.

 

This function is reserved for future use by Qt Designer

 
    The other two virtual functions can also be reimplemented

serial number

function

Return value description

11

initialize()

Set up custom widget extensions and other functions. Custom container extension (see QDesignerContainerExtension ) and task menu extension (see QDesignerTaskMenuExtension ) should be set in this function.

12

isInitialized()

If the component has been initialized, it returns true; otherwise, it returns false. The reimplementation usually checks whether the initialize() function has been called and returns the result of this test.

 

   
 
Notes for the domXml() function:
    The domXml() function returns a code snippet of a UI file, and uses the Qt  Designer window factory to create a custom widget and use features.
    Starting from Qt4.4, Qt  Designer 's widget allows a complete UI file to describe a custom widget. UI files can be loaded using tags. The specified tag allows you to add elements, which contain other information about the custom widget. If no more information is needed, then the label is sufficient.
    If the custom widget does not provide a reasonable size, it is necessary to specify the default position size (geometry) in the string returned by the domXml() function of the subclass. For example, the AnalogClockPlugin provided by the custom Widget plugin example uses the following method to specify the default widget position size:
 
    ...
           "  \n"
           "   \n"
           "    0\n"
           "    0\n"
           "    100\n"
           "    100\n"
           "   \n"
           "  \n"
    ...
    Another feature of the domXml() function is that if it returns an empty string, the widget will not be installed in the widget box of Qt  Designer . However, it can still be used by other forms of components. This feature is used to hide components and should not be explicitly created by the user, but needs to be created by other components.
     A complete custom part format is like this:
 displayname="MyWidget">
    
    
        
            widgets::MyWidget
            addPage
            
                
        
    

The attributes of the label:
Attributes Presentation form value content
language Optional "c++",
"jambi"
This attribute specifies the language provided by the custom widget.
It mainly prevents C++ plug-ins from appearing in Qt Jambi.
displayname Optional Class name The value of the attribute will appear in the widget box and can be used to strip the namespace.

 

    The tag tells Qt  Designer and UIC which method should be used to add the page to a container component. This applies to container components that need to call a specific method to add a child, instead of adding a child through the father. In particular, this is a related container, not a subclass container provided by Qt  Designer , but based on the concept of the current page. In addition, you need to provide a container to extend them.
    Elements can contain a list of meta-information for attributes. Currently, attributes of string type are supported. For these attributes, the tags can be used. The tag has the following attributes:
 
Attributes Presentation form value content
name necessary The name of the attribute
type have to See the table below The value of this attribute determines how the attribute editor will handle them.
notr Optional "true",
"false"
如果属性是“true”,则该值意味着不再被翻译。
字符串属性的类型属性的值:
类型
"richtext" 富文本
"multiline" 多行纯文本
"singleline" 单行纯文本
"stylesheet" 一个CSS样式表
"objectname" 对象名称(受限制的一组有效字符)
"url" URL、文件名.
插件要求
    为了让插件在所有平台上正常工作,你需要确保他们导出了Qt Designer所需要的符号。
    首先,插件类必须被Qt Designer加载的插件按顺序导出。使用 Q_PLUGIN_METADATA()宏来做到这一点。此外, QDESIGNER_WIDGET_EXPORT宏必须被使用,来定义Qt Designer将实例化的插件中每一个自定义窗口部件类。
 
创建一个良好的插件:
    一些自定义窗口部件有专门的用户界面功能,可以使他们的行为不同于Qt Designer中的其它标准窗口部件。特别是,如果一个自定义小部件由于调用QWidget::grabKeyboard()来捕获键盘,Qt Designer的操作将受到影响。
    为了让自定义部件在Qt Designer有特殊行为,提供 initialize()函数来配置窗口部件运行过程中的特定行为。该函数在被第一次调用之前先调用createWidget(),可以设定一个内部标志来测试什么时候调用createWidget()函数。
 

Guess you like

Origin blog.csdn.net/weixin_41882459/article/details/111832850