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)

创建一个动态 QDBusInterface 对象,该对象与服务service 上路径path 的对象上的接口interface 相关联,使用给定的connection 。如果interface 是空字符串,创建的对象将引用通过内省该对象找到的所有接口的合并。否则,如果interface 不是空字符串,QDBusInterface 对象将被缓存,以加快进一步创建相同接口的速度。

parent 将传递给基类构造函数。

如果远程服务service 不存在,或者在试图获取远程接口interface 的描述时发生错误,则创建的对象将无效(请参阅isValid() )。

[virtual noexcept] QDBusInterface::~QDBusInterface()

销毁对象界面,并释放已使用的资源。

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