桌面系统支持动态插件机制,你可以通过继承 DesktopPluginWidgetBase 接口,开发你自己的可视化 Card 插件,并部署到指定目录即可自动加载显示。
你需要继承提供的接口类 DesktopPluginWidgetBase,并实现其纯虚函数。
// MyCoolCardPlugin.h
#pragma once
#include "plugin_loader/plugin_base/DesktopPluginWidgetBase.h"
#include <QObject>
class MyCoolCardPlugin : public QObject, public DesktopPluginWidgetBase {
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.desktop.Plugin.CoolCard")
Q_INTERFACES(DesktopPluginWidgetBase)
public:
QWidget* factorize_widget(QWidget* parent = nullptr) override;
};
// MyCoolCardPlugin.cpp
#include "MyCoolCardPlugin.h"
#include <QLabel>
#include <QVBoxLayout>
QWidget* MyCoolCardPlugin::factorize_widget(QWidget* parent) {
QWidget* card = new QWidget(parent);
QVBoxLayout* layout = new QVBoxLayout(card);
layout->addWidget(new QLabel("✨ Hello from MyCoolCard!", card));
card->setLayout(layout);
return card;
}include(../../cmake/plugin_helper.cmake)
add_qt_plugin_with_json(WeatherCard WeatherCard "1.0"
SOURCES
./WeatherCard.h
./WeatherCard.cpp
./WeatherCard.ui
./WeatherRequestEngine.h
./WeatherRequestEngine.cpp
./WeatherData.h
./QueryCached.h
./QueryCached.cpp
./DesktopPluginWidgetBase.h
)
...其他的部分将生成的插件库文件放置到运行目录下的插件目录:
${DesktopRoot}/Runtimeplugins/
桌面系统会自动监控该文件夹,一旦检测到新的插件库文件(扩展名 .dll / .so / .dylib),将:
- 动态加载插件;
- 通过
QPluginLoader提取DesktopPluginWidgetBase实例; - 调用
factorize_widget()创建对应的可视控件; - 自动挂载到桌面或插件容器上。
- 插件必须使用
Q_PLUGIN_METADATA宏指定唯一的IID。 - 插件库名称推荐与类名一致,避免重名冲突。
- 插件需确保继承并正确实现
DesktopPluginWidgetBase接口。 - 插件运行时依赖 Qt 环境,请确认路径正确或使用 rpath / windeployqt 等方式解决依赖。
MyCoolCardPlugin/
├── CMakeLists.txt
├── MyCoolCardPlugin.h
├── MyCoolCardPlugin.cpp
└── build/
└── MyCoolCardPlugin.dll # 将此文件拷贝到 桌面的根文件夹/Runtime/plugins/The desktop system supports a dynamic plugin mechanism. By inheriting the DesktopPluginWidgetBase interface, you can develop your own visual card plugin and deploy it to a specified directory for automatic loading and display.
You need to inherit the provided interface class DesktopPluginWidgetBase and implement its pure virtual functions.
// MyCoolCardPlugin.h
#pragma once
#include "plugin_loader/plugin_base/DesktopPluginWidgetBase.h"
#include <QObject>
class MyCoolCardPlugin : public QObject, public DesktopPluginWidgetBase {
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.desktop.Plugin.CoolCard")
Q_INTERFACES(DesktopPluginWidgetBase)
public:
QWidget* factorize_widget(QWidget* parent = nullptr) override;
};
// MyCoolCardPlugin.cpp
#include "MyCoolCardPlugin.h"
#include <QLabel>
#include <QVBoxLayout>
QWidget* MyCoolCardPlugin::factorize_widget(QWidget* parent) {
QWidget* card = new QWidget(parent);
QVBoxLayout* layout = new QVBoxLayout(card);
layout->addWidget(new QLabel("✨ Hello from MyCoolCard!", card));
card->setLayout(layout);
return card;
}Example: Using the WeatherCard widget as an example, please refer to the built-in WeatherCard project template (CMake):
include(../../cmake/plugin_helper.cmake)
add_qt_plugin_with_json(WeatherCard WeatherCard "1.0"
SOURCES
./WeatherCard.h
./WeatherCard.cpp
./WeatherCard.ui
./WeatherRequestEngine.h
./WeatherRequestEngine.cpp
./WeatherData.h
./QueryCached.h
./QueryCached.cpp
./DesktopPluginWidgetBase.h
)
...Other SectionsPlace the generated plugin library file in the plugin directory under the runtime directory:
${DesktopRoot}/Runtimeplugins/
The desktop system automatically monitors this folder. Upon detecting a new plugin library file (with the extension .dll / .so / .dylib ), it will:
-
Dynamically load the plugin;
-
Extract the DesktopPluginWidgetBase file using QPluginLoader . Example;
-
Call
factorize_widget()to create the corresponding visual control; -
Automatically mount it to the desktop or plugin container.
- Plugins must use the
Q_PLUGIN_METADATAmacro to specify a uniqueIID. - It is recommended that the plugin library name be consistent with the class name to avoid name conflicts.
- Plugins must ensure that they inherit and correctly implement the
DesktopPluginWidgetBaseinterface. - Plugins rely on the Qt environment at runtime. Please ensure the path is correct or use methods such as
rpathorwindeployqtto resolve dependencies.
MyCoolCardPlugin/
├── CMakeLists.txt
├── MyCoolCardPlugin.h
├── MyCoolCardPlugin.cpp
└── build/
└── MyCoolCardPlugin.dll # Copy this file to the desktop root folder/Runtime/plugins/