本页内容

QDBusAbstractInterface Class

QDBusAbstractInterface 类是Qt D-Bus 绑定中所有 D-Bus 接口的基类,允许访问远程接口。更多内容...

头文件: #include <QDBusAbstractInterface>
CMake: find_package(Qt6 REQUIRED COMPONENTS DBus)
target_link_libraries(mytarget PRIVATE Qt6::DBus)
qmake: QT += dbus
继承自: QObject
继承自:

QDBusConnectionInterface 以及QDBusInterface

公共函数

virtual ~QDBusAbstractInterface()
QDBusPendingCall asyncCall(const QString &method, Args &&... args)
QDBusPendingCall asyncCallWithArgumentList(const QString &method, const QList<QVariant> &args)
QDBusMessage call(const QString &method, Args &&... args)
QDBusMessage call(QDBus::CallMode mode, const QString &method, Args &&... args)
QDBusMessage callWithArgumentList(QDBus::CallMode mode, const QString &method, const QList<QVariant> &args)
bool callWithCallback(const QString &method, const QList<QVariant> &args, QObject *receiver, const char *returnMethod, const char *errorMethod)
bool callWithCallback(const QString &method, const QList<QVariant> &args, QObject *receiver, const char *slot)
QDBusConnection connection() const
QString interface() const
(since 6.7) bool isInteractiveAuthorizationAllowed() const
bool isValid() const
QDBusError lastError() const
QString path() const
QString service() const
(since 6.7) void setInteractiveAuthorizationAllowed(bool enable)
void setTimeout(int timeout)
int timeout() const

详细说明

生成的代码类也继承自 QDBusAbstractInterface,此处描述的所有方法对生成的代码类同样适用。 除了此处描述的内容外,生成的代码类还为远程方法提供了成员函数,这些函数允许在编译时检查参数和返回值的正确性,以及属性类型匹配和信号参数匹配。

另请参阅 QDBus 编译器和 QDBusInterface 。

成员函数文档

[virtual noexcept] QDBusAbstractInterface::~QDBusAbstractInterface()

释放该对象的资源。

template <typename... Args> QDBusPendingCall QDBusAbstractInterface::asyncCall(const QString &method, Args &&... args)

调用该接口上的method 方法,并将args 作为参数传递给该方法。所有args 都必须能够转换为QVariant 。

call 的参数将通过 D-Bus 作为输入参数传递给远程函数。返回的QDBusPendingCall 对象可用于获取有关响应的信息。

其使用方法如下:

QDBusPendingCall pcall = interface->asyncCall("GetAPIVersion"_L1);
auto watcher = new QDBusPendingCallWatcher(pcall, this);

QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this,
                 [&](QDBusPendingCallWatcher *w) {
    QString value = retrieveValue();
    QDBusPendingReply<int> reply(*w);
    QDBusPendingCall pcall;
    if (reply.argumentAt<0>() >= 14)
        pcall = interface->asyncCall("ProcessWorkUnicode"_L1, value);
    else
        pcall = interface->asyncCall("ProcessWork"_L1, "UTF-8"_L1, value.toUtf8());

    w = new QDBusPendingCallWatcher(pcall);
    QObject::connect(w,  &QDBusPendingCallWatcher::finished, this,
                     &Abstract_DBus_Interface::callFinishedSlot);
});

此示例演示了带有 0、1 和 2 个参数的函数调用,并展示了每次传递的不同参数类型(对"ProcessWorkUnicode" 的第一次调用将包含一个 Unicode 字符串,对"ProcessWork" 的第二次调用将包含一个字符串和一个字节数组)。有关阻塞(同步)调用的相同示例,请参阅call()。

注意:在 Qt 5.14之前, 该函数最多仅接受八 (8) 个参数。

注意: 由于实现上的限制,对本地QDBusServer 的调用绝不会是异步的。

另请参阅 asyncCallWithArgumentList()。

QDBusPendingCall QDBusAbstractInterface::asyncCallWithArgumentList(const QString &method, const QList<QVariant> &args)

调用由method 指定的该接口上的远程方法,并将args 作为参数传入。该函数返回一个QDBusPendingCall 对象,可用于跟踪响应的状态,并在响应到达后访问其内容。

通常,您应使用asyncCall() 进行调用。

注意: 由于实现上的限制,对应用程序自身注册的对象的方法 调用绝不会是异步的。

注意:此函数是线程安全的。

template <typename... Args> QDBusMessage QDBusAbstractInterface::call(const QString &method, Args &&... args)

调用该接口上的method 方法,并将args 作为参数传递给该方法。所有args 都必须可转换为QVariant 。

call 的参数将通过 D-Bus 作为输入参数传递给远程函数。输出参数将在QDBusMessage 的响应中返回。如果响应为错误响应,则lastError() 也将被设置为错误消息的内容。

其使用方法如下:

QString value = retrieveValue();
QDBusMessage reply;

QDBusReply<int> api = interface->call("GetAPIVersion"_L1);
if (api >= 14)
  reply = interface->call("ProcessWorkUnicode"_L1, value);
else
  reply = interface->call("ProcessWork"_L1, "UTF-8"_L1, value.toUtf8());

此示例演示了带有 0、1 和 2 个参数的函数调用,并展示了每次传递的不同参数类型(对"ProcessWorkUnicode" 的第一次调用将包含一个 Unicode 字符串,对"ProcessWork" 的第二次调用将包含一个字符串和一个字节数组)。有关非阻塞(异步)调用的相同示例,请参阅asyncCall()。

注意:在 Qt 5.14之前, 该函数最多仅接受八 (8) 个参数。

另请参阅 callWithArgumentList()。

template <typename... Args> QDBusMessage QDBusAbstractInterface::call(QDBus::CallMode mode, const QString &method, Args &&... args)

调用该接口上的method 方法,并将args 作为参数传递给该方法。所有args 都必须可转换为QVariant 。

如果mode 为NoWaitForReply ,则该函数在发出调用后将立即返回,而不等待远程方法的响应。否则,mode 将决定该函数在等待响应期间是否应激活 Qt 事件循环。

如果该函数为了等待回复而重新进入 Qt 事件循环,则会排除用户输入。在等待期间,它可能会向您的应用程序传递信号和其他方法调用。因此,每当使用 call() 发起调用时,应用程序必须做好处理再入情况的准备。

注意:在 Qt 5.14之前, 此函数最多仅接受八(8)个参数。

这是一个重载函数。

另请参阅 callWithArgumentList()。

QDBusMessage QDBusAbstractInterface::callWithArgumentList(QDBus::CallMode mode, const QString &method, const QList<QVariant> &args)

使用args 作为参数,向该接口中由method 指定的远程方法发起调用。该函数返回作为响应接收到的消息,该消息可以是普通的QDBusMessage::ReplyMessage (表示成功),也可以是QDBusMessage::ErrorMessage (表示调用失败)。mode 参数指定了应如何发起此调用。

如果调用成功,lastError() 将被清空;否则,它将包含此次调用产生的错误。

通常,您应使用call() 进行调用。

警告:如果您 使用UseEventLoop ,您的代码必须准备好处理任何重入情况:在该函数返回之前,可能会收到其他方法调用和信号,以及其他 Qt 队列中的信号和事件。

注意:此函数是线程安全的。

bool QDBusAbstractInterface::callWithCallback(const QString &method, const QList<QVariant> &args, QObject *receiver, const char *returnMethod, const char *errorMethod)

使用args 作为参数,向该接口上由method 指定的远程方法发起调用。该函数在将调用入队后立即返回。远程函数的响应将传递给对象receiver 上的returnMethod 。如果发生错误,则改为调用对象receiver 上的errorMethod 。

如果入队成功,该函数返回true 。这并不表示执行的调用成功。如果失败,则调用errorMethod 。如果入队失败,该函数返回false ,且不会调用任何插槽。

returnMethod 的参数必须是函数调用返回的类型。可选地,它可以将QDBusMessage 作为最后一个或唯一的参数。errorMethod 必须将QDBusError 作为其唯一的参数。

注意: 由于实现限制,对应用程序自身注册的对象进行的方法 调用绝不会是异步的。

另请参阅 QDBusError 和QDBusMessage 。

bool QDBusAbstractInterface::callWithCallback(const QString &method, const QList<QVariant> &args, QObject *receiver, const char *slot)

此函数已弃用。请使用重载版本。

使用 `args ` 作为参数,将调用放入由 `method ` 指定的远程方法队列中。该函数在将调用放入队列后立即返回。远程函数的响应或其发出的任何错误都将传递至对象 `receiver` 上的 `slot ` 槽。

如果入队成功,该函数返回true :这并不表示调用成功。如果入队失败,则会向该槽传递一条错误消息。在此情况下,lastError() 不会被设置。

这是一个重载函数。

另请参阅 QDBusError 和QDBusMessage 。

QDBusConnection QDBusAbstractInterface::connection() const

返回与该接口关联的连接。

QString QDBusAbstractInterface::interface() const

返回此接口的名称。

[since 6.7] bool QDBusAbstractInterface::isInteractiveAuthorizationAllowed() const

返回在异步调用中,调用方是否准备好等待交互式授权。

默认值为false 。

该函数在 Qt 6.7 中引入。

另请参阅 setInteractiveAuthorizationAllowed() 和QDBusMessage::setInteractiveAuthorizationAllowed()。

bool QDBusAbstractInterface::isValid() const

如果这是一个对远程对象的有效引用,则返回true 。如果在创建此接口时发生错误(例如,远程应用程序不存在),则返回false 。

注意:在处理远程对象时,创建QDBusInterface 时并不总能确定该对象是否存在。

QDBusError QDBusAbstractInterface::lastError() const

返回上次操作产生的错误;如果上次操作未产生错误,则返回一个无效错误。

QString QDBusAbstractInterface::path() const

返回与该接口关联的对象路径。

QString QDBusAbstractInterface::service() const

返回此接口所关联的服务的名称。

[since 6.7] void QDBusAbstractInterface::setInteractiveAuthorizationAllowed(bool enable)

配置在异步调用时,调用方是否愿意等待交互式授权。

如果将enable 设置为true ,则通过此接口为异步调用生成的 D-Bus 消息将设置ALLOW_INTERACTIVE_AUTHORIZATION 标志。

只有当无特权代码调用更高特权的方法,并且部署了允许交互式授权的授权框架时,此标志才有用。

默认值为false 。

该函数在 Qt 6.7 中引入。

另请参阅 isInteractiveAuthorizationAllowed() 和QDBusMessage::setInteractiveAuthorizationAllowed()。

void QDBusAbstractInterface::setTimeout(int timeout)

将所有未来对 `timeout` 的 DBus 调用的超时时间设置为毫秒。-1 表示默认的 DBus 超时时间(通常为 25 秒)。

另请参阅 timeout()。

int QDBusAbstractInterface::timeout() const

返回超时时间的当前值(单位为毫秒)。-1 表示默认的 DBus 超时时间(通常为 25 秒)。

另请参阅 setTimeout()。

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