qt 5.14.1 mqtt正常编译分享

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

qt 5.14.1 mqtt正常编译分享

  • 1 qt 5.14.1 mqtt编译
  • 2 fatal error: QtMqtt/qmqttglobal.h: No such file or directory
    • 2.1 解决方案
  • 3 QMqttClient Class常用接口
    • 3.1 常用信号 Signals
    • 3.2 常用方法
    • 3.3 测试代码
      • 3.3.1 连接
      • 3.3.2 发布
      • 3.3.3 订阅
      • 3.3.4 接收消息

原文链接:
https://blog.csdn.net/qq_42670606/article/details/107655881

1 qt 5.14.1 mqtt编译

一、csdn上绝大部分文章和下载都是骗分的,这个是事实,这是为什么呢?

qtmqtt 有很多版本,对应的源码是不相同的,比如我用的5.14.1 ,下载的是最新的mqtt编译。
qt 5.14.1 mqtt正常编译分享_第1张图片
从 上面图可以看到最新的mqtt更新,这也是为什么最新的mqtt编译后,就没有libQt5Qmqttd.a 和Qt5Qmqttd.dll文件的原因,根本就没有两个a和两个dll文件,下面是我编译出来的可以用文件,我把这个编译的文件后面上传,初学者可以不用再编译。

qt 5.14.1 mqtt正常编译分享_第2张图片
qt 5.14.1 mqtt正常编译分享_第3张图片
qt 5.14.1 mqtt正常编译分享_第4张图片
qt 5.14.1 mqtt正常编译分享_第5张图片

二、采用QT内置模块现在CSDN上的阐述都是错误的,因为

QT       += mqtt 根本就不是正确的模块名称,正确的是 QT       += qmqtt

这一个好像有问题,我用qmqtt编译也是提示没有这个模块
qt 5.14.1 mqtt正常编译分享_第6张图片

2 fatal error: QtMqtt/qmqttglobal.h: No such file or directory

打开最外层的qtmqtt.pro项目文件,构建中会包文件找不到,如 等文件,
主要原件是编译器是按照在Qt安装目录下搜索的,但我们文件是我们自己随意放的目录下的 src\mqtt 路径下。

2.1 解决方案

在安装目下include下创建QtMqtt文件夹,例如

C:\Qt\Qt5.14.2\5.14.2\mingw73_32\include\QtMqtt

将源文件src/mqtt下所有.h头文件添加到我们创建的QtMqtt文件夹。
qt 5.14.1 mqtt正常编译分享_第7张图片
qt 5.14.1 mqtt正常编译分享_第8张图片

3 QMqttClient Class常用接口

The QMqttClient class represents the central access communicating with an MQTT broker.

Header:	#include <QMqttClient>
qmake:	QT += mqtt
Inherits:	QObject

3.1 常用信号 Signals

//This signal is emitted when a connection has been established.
void	connected()//This signal is emitted when a connection has been closed. A connection may be closed when disconnectFromHost() is called or when the broker disconnects.
void	disconnected()//This signal is emitted after the broker responds to a requestPing() call or a keepAlive() ping message, and the connection is still valid.
void	pingResponseReceived()/**state : ClientState
This property holds the current state of the client.
QMqttClient::ClientState	state() const
void	setState(QMqttClient::ClientState state);
Notifier signal:
void	stateChanged(QMqttClient::ClientState state)
*/
void	stateChanged(QMqttClient::ClientState state)//This signal is emitted when a new message has been received. The category of the message is specified by topic with the content being message.
void	messageReceived(const QByteArray &message, const QMqttTopicName &topic = QMqttTopicName())

3.2 常用方法

qint32 QMqttClient::publish(const QMqttTopicName &topic, const QByteArray &message = QByteArray(), quint8 qos = 0, bool retain = false)

Publishes a message to the broker with the specified topic. qos specifies the level of security required for transferring the message.

If retain is set to true, the message will stay on the broker for other clients to connect and receive the message.

Returns an ID that is used internally to identify the message.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

qint32 QMqttClient::publish(const QMqttTopicName &topic, const QMqttPublishProperties &properties, const QByteArray &message = QByteArray(), quint8 qos = 0, bool retain = false)

Publishes a message to the broker with the specified properties and topic. qos specifies the level of security required for transferring the message.

If retain is set to true, the message will stay on the broker for other clients to connect and receive the message.

Returns an ID that is used internally to identify the message.

Note: properties will only be passed to the broker when the client specifies MQTT_5_0 as ProtocolVersion.

This function was introduced in Qt 5.12.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

QMqttSubscription *QMqttClient::subscribe(const QMqttTopicFilter &topic, quint8 qos = 0)

Adds a new subscription to receive notifications on topic. The parameter qos specifies the level at which security messages are received. For more information about the available QoS levels, see Quality of Service.

This function returns a pointer to a QMqttSubscription. If the same topic is subscribed twice, the return value points to the same subscription instance. The MQTT client is the owner of the subscription.

QMqttSubscription *QMqttClient::subscribe(const QMqttTopicFilter &topic, const QMqttSubscriptionProperties &properties, quint8 qos = 0)

Adds a new subscription to receive notifications on topic. The parameter properties specifies additional subscription properties to be validated by the broker. The parameter qos specifies the level at which security messages are received. For more information about the available QoS levels, see Quality of Service.

This function returns a pointer to a QMqttSubscription. If the same topic is subscribed twice, the return value points to the same subscription instance. The MQTT client is the owner of the subscription.

Note: properties will only be passed to the broker when the client specifies MQTT_5_0 as ProtocolVersion.

This function was introduced in Qt 5.12.

qt 5.14.1 mqtt正常编译分享_第9张图片
https://doc.qt.io/QtMQTT/qmqttclient.html#details

3.3 测试代码

3.3.1 连接

//连接
void MainWindow::on_pushButton_connect_clicked()
{
    if (m_client->state() == QMqttClient::Disconnected)
    {
        ui->pushButton_connect->setText(tr("断开"));
        m_client->setHostname(ui->lineEdit_host->text());
        m_client->setPort(ui->lineEdit_port->text().toInt());
        m_client->setUsername(ui->lineEdit_user->text());
        m_client->setPassword(ui->lineEdit_pwd->text());
        m_client->setClientId("1234");

        m_client->setKeepAlive(120);

        ui->lineEdit_host->setEnabled(false);
        ui->lineEdit_port->setEnabled(false);
        ui->lineEdit_user->setEnabled(false);
        ui->lineEdit_pwd->setEnabled(false);

        m_client->connectToHost();
    }
    else
    {//断开连接
        ui->pushButton_connect->setText(tr("连接"));
        ui->lineEdit_host->setEnabled(true);
        ui->lineEdit_port->setEnabled(true);
        ui->lineEdit_user->setEnabled(true);
        ui->lineEdit_pwd->setEnabled(true);

        m_client->disconnectFromHost();
        ui->textEdit->append("连接断开!!!");
    }
}

3.3.2 发布

//发布
void MainWindow::on_pushButton_2_clicked()
{
    QString topicPub;
    QByteArray messagePub;
    if(m_client->state() == QMqttClient::Connected)
    {
        topicPub = ui->lineEdit_pub->text();
        messagePub = ui->lineEdit_pubMsg->text().toUtf8();

        m_client->publish(topicPub, messagePub, 0);
        return;
    }
    ui->textEdit->append("请连接后重新操作!");
}

3.3.3 订阅

//订阅
void MainWindow::on_pushButton_clicked()
{
    QString topicSub;
    int qos;
    if(m_client->state() == QMqttClient::Connected)
    {
        topicSub = ui->lineEdit_sub->text();
        qos = ui->spinBox->text().toInt();
        m_client->subscribe(topicSub, qos);
        return;
    }
    ui->textEdit->append("请连接后重新操作!");
}

3.3.4 接收消息

//收到消息
void MainWindow::topicMessageReceived(const QByteArray &message, const QMqttTopicName &topic)
{
    QString content;
    content = QDateTime::currentDateTime().toString() + QLatin1Char('\n');
    content += QLatin1String(" Received Topic: ") + topic.name() + QLatin1Char('\n');
    content += QLatin1String(" Message: ") + message + QLatin1Char('\n');
    ui->textEdit->append(content);
}


Qt开发技术:mqtt介绍、QtMqtt编译和开发环境搭建
https://blog.csdn.net/qq21497936/article/details/91463870

你可能感兴趣的:(Qt经验总结,qt,物联网,开发语言)