本页内容

QMqttSubscription Class

QMqttSubscription 类用于接收来自 MQTT 代理关于指定主题的通知。更多内容...

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

公共类型

enum SubscriptionState { Unsubscribed, SubscriptionPending, Subscribed, UnsubscriptionPending, Error }

属性

公共函数

virtual ~QMqttSubscription() override
bool isSharedSubscription() const
quint8 qos() const
QString reason() const
QMqtt::ReasonCode reasonCode() const
QString sharedSubscriptionName() const
QMqttSubscription::SubscriptionState state() const
QMqttTopicFilter topic() const
QMqttUserProperties userProperties() const

公共插槽

void unsubscribe()

信号

void messageReceived(QMqttMessage msg)
void qosChanged(quint8)
void stateChanged(QMqttSubscription::SubscriptionState state)

详细说明

成员类型文档

enum QMqttSubscription::SubscriptionState

此枚举类型描述了订阅可能处于的状态。

常量值描述
QMqttSubscription::Unsubscribed0已取消对该主题的订阅。
QMqttSubscription::SubscriptionPending1已发送订阅请求,但尚未得到代理的确认。
QMqttSubscription::Subscribed2订阅已成功,将接收消息。
QMqttSubscription::UnsubscriptionPending3已发送取消订阅该主题的请求,但尚未得到经纪商的确认。
QMqttSubscription::Error4发生错误。

属性文档

[read-only] qos : quint8

此属性保存订阅时设定的 QoS 级别。

订阅的 QoS 级别指定了客户端接收消息时的最高QoS 级别。发布者仍可发送级别较低的消息。

访问函数:

quint8 qos() const

通知信号:

void qosChanged(quint8)

[read-only] reason : QString

该属性存储订阅的理由字符串。

服务器使用原因字符串来提供有关订阅的附加信息。服务器是否发送该字符串是可选的。

访问函数:

QString reason() const

[read-only] reasonCode : QMqtt::ReasonCode

该属性存储订阅的错误代码。

如果订阅失败,原因代码会指定错误类型;如果订阅成功,则指定服务质量(QoS)级别。

访问函数:

QMqtt::ReasonCode reasonCode() const

[read-only] sharedSubscription : bool

该属性用于标识订阅是否为共享订阅。

访问函数:

bool isSharedSubscription() const

[read-only] sharedSubscriptionName : QString

该属性存储共享订阅的名称。

访问函数:

QString sharedSubscriptionName() const

[read-only] state : SubscriptionState

该属性保存订阅的状态。

访问函数:

QMqttSubscription::SubscriptionState state() const

通知信号:

void stateChanged(QMqttSubscription::SubscriptionState state)

[read-only] topic : QMqttTopicFilter

该属性存储订阅的主题。

访问函数:

QMqttTopicFilter topic() const

成员函数文档

[override virtual noexcept] QMqttSubscription::~QMqttSubscription()

删除一个订阅。如果尚未取消订阅topic ,系统将自动取消该订阅。

[signal] void QMqttSubscription::messageReceived(QMqttMessage msg)

当接收到新消息msg 时,会发出此信号。

[slot] void QMqttSubscription::unsubscribe()

取消订阅topic 。

注意:这 可能会影响QMqttClient::subscribe() 返回的所有共享指针实例。

QMqttUserProperties QMqttSubscription::userProperties() const

返回订阅被接受时从经纪商处接收到的用户属性。

注意: 仅当客户端将QMqttClient::MQTT_5_0 指定为QMqttClient::ProtocolVersion 时,此 函数才会提供有效数据。

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