使用Qt D-Bus 适配程序

适配器是附加到任何QObject 派生类的特殊类,使用 D-Bus 为外部世界提供接口。适配器是一种轻量级类,其主要目的是中继与真实对象之间的调用,并可能验证或转换来自外部世界的输入,从而保护真实对象。

与多重继承不同,适配器可以随时添加到任何对象中(但不能移除),因此在导出现有类时具有更大的灵活性。适配器的另一个优点是在不同接口的同名方法中提供相似但不完全相同的功能,这种情况在为一个对象添加新版本的标准接口时很常见。

要使用适配器,必须创建一个继承了QDBusAbstractAdaptor 的类。由于这是一个标准的QObject 继承类,因此Q_OBJECT 宏必须出现在声明中,源文件必须使用moc工具处理。该类还必须包含一个Q_CLASSINFO 条目,名称为"D-Bus Interface" ,声明它导出了哪个接口。每个类只支持一个条目。

类中的任何公共槽都可以通过总线上的 MethodCall 类型消息进行访问。(更多信息请参阅 "在 D-Bus 适配器中声明插槽")。类中的信号将自动通过 D-Bus 中转。不过,并非所有类型都允许使用信号或插槽参数列表:更多信息请参阅 Qt D-Bus 类型系统

此外,使用Q_PROPERTY 声明的任何属性都将自动通过 D-Bus 上的属性接口公开。由于QObject 属性系统不允许非可读属性,因此无法使用适配器声明只写属性。

更多信息:

另请参见 QDBusAbstractAdaptor

© 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.