QDBusInterface Class
QDBusInterface 类是远程对象上接口的代理。更多内容...
| 头文件: | #include <QDBusInterface> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS DBus) target_link_libraries(mytarget PRIVATE Qt6::DBus) |
| qmake: | QT += dbus |
| 继承自: | QDBusAbstractInterface |
公共函数
| QDBusInterface(const QString &service, const QString &path, const QString &interface = QString(), const QDBusConnection &connection = QDBusConnection::sessionBus(), QObject *parent = nullptr) | |
| virtual | ~QDBusInterface() |
详细说明
QDBusInterface 是一个通用的访问器类,用于调用远程对象、订阅远程对象导出的信号以及获取/设置远程属性的值。当您没有代表远程接口的生成代码时,该类可用于动态访问远程对象。
调用通常通过call()函数实现,该函数负责构建消息、通过总线发送消息、等待响应并解码响应。信号连接则使用常规的QObject::connect()函数。最后,属性访问通过QObject::property()和QObject::setProperty()函数实现。
以下代码片段演示了如何在名为com.example.Calculator 的远程应用程序中执行"2 + 2" 数学运算,该应用程序通过会话总线访问。
QDBusInterface remoteApp( "com.example.Calculator", "/Calculator/Operations",
"org.mathematics.RPNCalculator" );
remoteApp.call( "PushOperand", 2 );
remoteApp.call( "PushOperand", 2 );
remoteApp.call( "ExecuteOperation", "+" );
QDBusReply<int> reply = remoteApp.call( "PopOperand" );
if ( reply.isValid() )
printf( "%d", reply.value() ); // prints 4另请参阅 Qt D-Bus XML 编译器 (qdbusxml2cpp)。
成员函数文档
QDBusInterface::QDBusInterface(const QString &service, const QString &path, const QString &interface = QString(), const QDBusConnection &connection = QDBusConnection::sessionBus(), QObject *parent = nullptr)
使用给定的connection ,创建一个动态的QDBusInterface对象,该对象与路径path 上服务service 中的interface 接口相关联。如果interface 为空字符串,则创建的对象将引用通过内省该对象所找到的所有接口的合并结果。 否则,如果interface 不为空,则会将 QDBusInterface 对象缓存起来,以加快后续创建相同接口的速度。
parent 将传递给基类构造函数。
如果远程服务service 不存在,或者在尝试获取远程接口interface 的描述时发生错误,则创建的对象将无效(参见isValid())。
[virtual noexcept] QDBusInterface::~QDBusInterface()
销毁该对象接口,并释放其占用的所有资源。
© 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.