本页内容

QDBusServiceWatcher Class

QDBusServiceWatcher 类允许用户监听总线服务的变更。更多内容...

标题: #include <QDBusServiceWatcher>
CMake: find_package(Qt6 REQUIRED COMPONENTS DBus)
target_link_libraries(mytarget PRIVATE Qt6::DBus)
qmake: QT += dbus
继承自: QObject

公共类型

flags WatchMode
enum WatchModeFlag { WatchForRegistration, WatchForUnregistration, WatchForOwnerChange }

属性

公共函数

QDBusServiceWatcher(QObject *parent = nullptr)
QDBusServiceWatcher(const QString &service, const QDBusConnection &connection, QDBusServiceWatcher::WatchMode watchMode = WatchForOwnerChange, QObject *parent = nullptr)
virtual ~QDBusServiceWatcher()
void addWatchedService(const QString &newService)
QBindable<QDBusServiceWatcher::WatchMode> bindableWatchMode()
QBindable<QStringList> bindableWatchedServices()
QDBusConnection connection() const
bool removeWatchedService(const QString &service)
void setConnection(const QDBusConnection &connection)
void setWatchMode(QDBusServiceWatcher::WatchMode mode)
void setWatchedServices(const QStringList &services)
QDBusServiceWatcher::WatchMode watchMode() const
QStringList watchedServices() const

信号

void serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner)
void serviceRegistered(const QString &serviceName)
void serviceUnregistered(const QString &serviceName)

详细说明

QDBusServiceWatcher 对象可用于在总线上某项服务名称的所有权发生变更时通知应用程序。它有三种监视模式:

  • 仅监听服务注册。
  • 仅监视服务注销。
  • 监听任何类型的服务所有权变更(默认模式)。

除了被创建或删除外,服务也可能在未发生注销/注册操作的情况下变更所有者。因此,若发生此类情况,serviceRegistered() 和serviceUnregistered() 信号可能不会被触发。

与使用 QDBusConnectionInterface::serviceOwnerChanged() 信号相比,该类效率更高,因为它允许用户仅接收该类感兴趣的信号。

服务名称以字符“*”结尾时,将匹配指定命名空间内的所有服务名称。

例如,“com.example.backend1*”将匹配

  • com.example.backend1
  • com.example.backend1.foo
  • com.example.backend1.foo.bar

同一域中的子字符串不会被匹配,例如“com.example.backend12”。

另请参阅 QDBusConnection 。

成员类型文档

enum QDBusServiceWatcher::WatchModeFlag
flags QDBusServiceWatcher::WatchMode

QDBusServiceWatcher 支持三种不同的监视模式,可通过以下标志进行配置:

常量值描述
QDBusServiceWatcher::WatchForRegistration0x01仅监听服务注册,忽略与服务所有权变更相关的任何信号。
QDBusServiceWatcher::WatchForUnregistration0x02仅监视服务注销,忽略与服务所有权变更相关的任何信号。
QDBusServiceWatcher::WatchForOwnerChange0x03监听任何类型的服务所有权变更。

WatchMode 类型是QFlags<WatchModeFlag> 的 typedef。它存储 WatchModeFlag 值的按“或”运算组合。

属性文档

[bindable] watchMode : WatchMode

注意:此 属性支持QProperty 绑定。

该属性保存了此QDBusServiceWatcher 对象的当前监视模式。

该属性的默认值为 QDBusServiceWatcher::WatchForOwnershipChange。

访问函数:

QDBusServiceWatcher::WatchMode watchMode() const
void setWatchMode(QDBusServiceWatcher::WatchMode mode)

[bindable] watchedServices : QStringList

注意:此 属性支持QProperty 绑定。

该属性保存被监听的服务列表。

注意: 使用 setServicesWatched()修改 此列表是一项开销较大的操作。如果可能,请优先通过addWatchedService() 和removeWatchedService() 进行修改。

访问函数:

QStringList watchedServices() const
void setWatchedServices(const QStringList &services)

成员函数文档

[explicit] QDBusServiceWatcher::QDBusServiceWatcher(QObject *parent = nullptr)

创建一个 QDBusServiceWatcher 对象。请注意,在通过setConnection() 建立连接之前,该对象不会发出任何信号。

parent 参数将传递给QObject 方法,用于设置该对象的父对象。

QDBusServiceWatcher::QDBusServiceWatcher(const QString &service, const QDBusConnection &connection, QDBusServiceWatcher::WatchMode watchMode = WatchForOwnerChange, QObject *parent = nullptr)

创建一个 QDBusServiceWatcher 对象,并将其附加到connection 连接上。此外,该函数会立即开始监听服务service 的watchMode 变更。

参数parent 将传递给QObject ,用于设置该对象的父对象。

[virtual noexcept] QDBusServiceWatcher::~QDBusServiceWatcher()

销毁QDBusServiceWatcher 对象,并释放与其关联的所有资源。

void QDBusServiceWatcher::addWatchedService(const QString &newService)

将newService 添加到该对象需监听的服务列表中。此函数比setWatchedServices() 更高效,在添加服务时应尽可能使用该函数。

移除watchedServices 的任何现有绑定。

QDBusConnection QDBusServiceWatcher::connection() const

返回此对象所关联的QDBusConnection 。

另请参阅 setConnection()。

bool QDBusServiceWatcher::removeWatchedService(const QString &service)

将service 从该对象监听的服务列表中移除。请注意,D-Bus通知是异步的,因此可能仍有关于service 的待发送信号。每当处理D-Bus消息时,这些信号仍会被发出。

移除watchedServices 的任何现有绑定。

如果移除了任何服务,该函数将返回true 。

[signal] void QDBusServiceWatcher::serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner)

每当该对象检测到与serviceName 服务相关的服务所有权发生变更时,就会触发此信号。oldOwner 参数包含旧所有者的名称,newOwner 则是新所有者。oldOwner 和newOwner 均为唯一的连接名称。

请注意,每当serviceName 服务被注册或注销时,也会触发此信号。如果服务被注册,oldOwner 将包含一个空字符串;而如果服务被注销,newOwner 将包含一个空字符串。

如果您仅需判断服务是否已注册或已注销,而无需接收所有权变更的通知,请考虑使用针对这些操作的特定模式。若使用更具体的模式,本类的运行效率会更高。

另请参阅 serviceRegistered() 和serviceUnregistered()。

[signal] void QDBusServiceWatcher::serviceRegistered(const QString &serviceName)

每当该对象检测到总线上serviceName 服务已可用时,就会发出此信号。

另请参阅 serviceUnregistered() 和serviceOwnerChanged()。

[signal] void QDBusServiceWatcher::serviceUnregistered(const QString &serviceName)

每当该对象检测到服务serviceName 已从总线注销且不可用时,就会发出此信号。

另请参阅 serviceRegistered() 和serviceOwnerChanged()。

void QDBusServiceWatcher::setConnection(const QDBusConnection &connection)

将该对象所关联的 D-Bus 连接设置为connection 。所有被监听的服务都将转移到此连接上。

请注意,QDBusConnection 对象采用引用计数机制:QDBusServiceWatcher 会在该连接存在期间保持对其的引用。该连接在引用计数降为零之前不会关闭,因此这可确保在该QDBusServiceWatcher 对象存在期间能接收到所有通知。

另请参阅 connection()。

void QDBusServiceWatcher::setWatchedServices(const QStringList &services)

将正在监视的 D-Bus 服务列表设置为services 。

请注意,设置整个列表意味着将删除所有先前用于监视服务的规则,并添加新的规则。这是一项开销较大的操作,应尽可能避免。相反,如果可能的话,请使用addWatchedService() 和removeWatchedService() 来操作列表中的条目。

移除watchedServices 的任何现有绑定。

注意: 这是属性watchedServices 的设置 函数。

另请参阅 watchedServices()。

QStringList QDBusServiceWatcher::watchedServices() const

返回正在监视的 D-Bus 服务列表。

注意: 这是属性 watchedServices 的获取 函数。

另请参阅 setWatchedServices()。

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