QDesignerCustomWidgetCollectionInterface Class
QDesignerCustomWidgetCollectionInterface 类允许您将多个自定义控件包含在一个库中。更多内容...
| 头文件: | #include <QDesignerCustomWidgetCollectionInterface> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Designer) target_link_libraries(mytarget PRIVATE Qt6::Designer) |
| qmake: | QT += designer |
公共函数
| virtual | ~QDesignerCustomWidgetCollectionInterface() |
| virtual QList<QDesignerCustomWidgetInterface *> | customWidgets() const = 0 |
详细说明
在实现自定义控件插件时,需将其构建为独立的库。若要在同一个库中包含多个自定义控件插件,还必须继承 QDesignerCustomWidgetCollectionInterface 接口。
QDesignerCustomWidgetCollectionInterface 包含一个函数,该函数返回集合中所有 `QDesignerCustomWidgetInterface ` 对象的列表。例如,如果你有几个自定义控件 `CustomWidgetOne`、`CustomWidgetTwo ` 和 `CustomWidgetThree`,类定义可能如下所示:
#include customwidgetoneinterface.h
#include customwidgettwointerface.h
#include customwidgetthreeinterface.h
#include <QtDesigner/qtdesigner.h>
#include <QtCore/qplugin.h>
class MyCustomWidgets: public QObject, public QDesignerCustomWidgetCollectionInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetCollectionInterface")
Q_INTERFACES(QDesignerCustomWidgetCollectionInterface)
public:
MyCustomWidgets(QObject *parent = 0);
QList<QDesignerCustomWidgetInterface*> customWidgets() const override;
private:
QList<QDesignerCustomWidgetInterface*> widgets;
};在类构造函数中,将自定义控件的接口添加到列表中,该列表将在customWidgets() 函数中返回:
MyCustomWidgets::MyCustomWidgets(QObject *parent)
: QObject(parent)
{
widgets.append(new CustomWidgetOneInterface(this));
widgets.append(new CustomWidgetTwoInterface(this));
widgets.append(new CustomWidgetThreeInterface(this));
}
QList<QDesignerCustomWidgetInterface*> MyCustomWidgets::customWidgets() const
{
return widgets;
}请注意,您不应使用Q_PLUGIN_METADATA() 宏分别导出每个自定义小部件插件,而是应导出整个集合。Q_PLUGIN_METADATA() 宏可确保Qt Widgets Designer 能够访问并构造这些自定义小部件。如果没有此宏,Qt Widgets Designer 将无法使用它们。
另请参阅 QDesignerCustomWidgetInterface 以及《为Qt Widgets Designer 创建自定义控件》。
成员函数文档
[virtual constexpr noexcept default] QDesignerCustomWidgetCollectionInterface::~QDesignerCustomWidgetCollectionInterface()
销毁自定义小部件集合接口。
[pure virtual] QList<QDesignerCustomWidgetInterface *> QDesignerCustomWidgetCollectionInterface::customWidgets() const
返回该集合的自定义控件的接口列表。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.