QDesignerCustomWidgetCollectionInterface Class
QDesignerCustomWidgetCollectionInterface 类允许您在一个库中包含多个自定义部件。更多
Header: | #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 |
详细说明
在实现自定义 widget 插件时,您需要将其构建为一个单独的库。如果您想在同一个库中包含多个自定义 widget 插件,则必须另外子类化 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() 宏导出每个自定义 widget 插件,而是导出整个集合。Q_PLUGIN_METADATA() 宏确保Qt Widgets Designer 可以访问和构建自定义部件。没有这个宏,Qt Widgets Designer 就无法使用它们。
另请参阅 QDesignerCustomWidgetInterface 和为Qt Widgets Designer 创建自定义部件。
成员函数文档
[virtual constexpr noexcept]
QDesignerCustomWidgetCollectionInterface::~QDesignerCustomWidgetCollectionInterface()
销毁自定义 widget 收集界面。
[pure virtual]
QList<QDesignerCustomWidgetInterface *> QDesignerCustomWidgetCollectionInterface::customWidgets() const
返回自定义部件集合的接口列表。
© 2025 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.