【Qt官方MQTT库的使用,附一个MqttClient例子】

时间:2024-04-17 13:40:25

Qt官方MQTT库的使用,附一个MqttClient例子

修改説明:

修改時間:2019年5月31日

修改内容:mqtt庫編譯的説明

-----------------------------------------------------------

开发环境:win7 64 + Qt5.9

记录时间:2018年3月11日 00:48:42

联系邮箱: yexiaopeng1992@126.com(有问题发我邮件)

一:前言

  最近在调试Mqtt通讯基础上的加密,但因为Mqtt服务器是大家公用的,不能修改,所以就决定写一个Mqtt的客户端,与自己的开发板之间通信,使用客户端模拟服务器的加密。因为自己在图形编程中,自己对Qt比较熟悉,因此选择了Qt进行编程。但后来发现Qt没有集成Mqtt的组件,而使用网上大部分网友说说的emqtt作为Mqtt库的话,虽然编译没有问题,但发现使用qmqtt虽然可以连接公司的Mqtt服务器,但不能通讯。后来上Y_o_u_T_u_b_e搜索了一下,看见有Qt官方介绍Mqtt的视频,研究并写了一个小Demo测试,发现可以使用,在文末会提供下载测试。在此,便简单介绍一下,更多功能欢迎大家一起研究。

二:资料

介绍地址:https://codereview.qt-project.org/#/admin/projects/qt/qtmqtt
源码下载:git clone https://codereview.qt-project.org/qt/qtmqtt
Qt MQTT Examples:https://doc.qt.io/QtMQTT/qtmqtt-examples.html
Qt MQTT Client Example:https://doc.qt.io/QtMQTT/qtmqtt-simpleclient-example.html

三:编译生成QtMqtt的库

修改説明

當前Qt已經將MQTT的代碼上傳至github,在使用這個版本進行庫生成時,有些許差異。特此説明

github地址:https://github.com/qt/qtmqtt 

下載后,打開最外層的 qtmqtt.pro項目進行構建即可。  構建中會報文件找不到的錯誤。如  <QtMqtt/qmqttglobal.h>等文件,

按照這種寫法,文件應該在Qt按照路徑下,但文件是存在 項目的 src\mqtt 路徑下。因此,在Qt安裝路徑的頭文件目錄下,新建QtMqtt目錄,將對應文件放入,重新編譯即可得到庫。


 

 

3.1  git clone https://codereview.qt-project.org/qt/qtmqtt 下载源码到本地

3.2 打开qtmqtt文件夹下的qtmqtt.pro工程

3.3 重新构建qtmqtt.pro

3.4 构建成功后,将输出文件夹中的include和lib文件夹复制到其他地方

3.4 将lib文件夹下除了libQt5Mqtt.a、libQt5Mqttd.a、Qt5Mqtt.dll、Qt5Mqttd.dll四个文件之外的文件删除

3.5 将include\QtMqtt 文件下除了qtmqttversion.h以外的.h文件,全部用qtmqtt\src\mqtt中同名的.h文件取代(include内的头文件只是指向了

qtmqtt\src\mqtt中的头文件)

四:新建自己的工程来使用QtMqtt的库

4.1 新建一个Qt工程,基于Qidget,不使用系统生成UI

4.2将第三步获得的lib和include文件夹复制到新工程的文件夹下。

4.3 修改.pro文件:

增加network模块
QT += core gui network

增加头文件搜索路径
INCLUDEPATH += ./include

增加库

4.4 根据 https://doc.qt.io/QtMQTT/qtmqtt-simpleclient-example.html 中的例子,简单写了一个先服务器发布消息的小Demo

 

五:代码

 1 //main.cpp
 2 #include "QMqttClientTool.h"
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     QMqttClientTool w;
 9     w.show();
10 
11     return a.exec();
12 }
//QMqttClientTool.h
#ifndef QMQTTCLIENTTOOL_H
#define QMQTTCLIENTTOOL_H

#include <QWidget>
#include "QtMqtt/QMqttClient"
#include "QPushButton"
#include "QLineEdit"
#include "QLabel"

class QMqttClientTool : public QWidget
{
    Q_OBJECT

public:
    QMqttClientTool(QWidget *parent = 0);
    ~QMqttClientTool();

    QPushButton * btnConnect;//连接MQTT服务器
    QPushButton * btnPublish;//发布消息
    QLabel * qlbHostNameTag;
    QLabel * qlbHostPortTag;
    QLabel * qlbPubLishTopicTag;
    QLabel * qlbPublishMessageTag;
    QLineEdit * qleHostName;
    QLineEdit * qleHostPort;
    QLineEdit * qlePublishTopic;
    QLineEdit * qlePublishMessage;

private:
    QMqttClient * m_client;//mqtt client指针

private slots:
    void on_btnConnect_clicked(void);//连接MQTT服务器槽函数
    void on_btnPublish_clicked(void);//发布消息槽函数

};

#endif // QMQTTCLIENTTOOL_H
//QMqttClientTool.cpp
#include "QMqttClientTool.h"
#include "QDebug"
#include <QtWidgets/QMessageBox>
QMqttClientTool::QMqttClientTool(QWidget *parent)
    : QWidget(parent)
{

    this->setMaximumSize(600,200);
    this->setMinimumSize(600,200);

    m_client             =  new QMqttClient(this);
    btnConnect           =  new QPushButton(this);
    btnPublish           =  new QPushButton(this);
    qlbHostNameTag       =  new QLabel(this);
    qlbHostPortTag       =  new QLabel(this);
    qlbPubLishTopicTag   =  new QLabel(this);
    qlbPublishMessageTag =  new QLabel(this);
    qleHostName          =  new QLineEdit(this);
    qleHostPort          =  new QLineEdit(this);
    qlePublishTopic      =  new QLineEdit(this);
    qlePublishMessage    =  new QLineEdit(this);
    QFont ft;
    ft.setPointSize(15);
    ft.setBold(1);

    qlbHostNameTag->move(6,6);
    qlbHostNameTag->setText("Host");
    qlbHostNameTag->setFont(ft);
    qlbHostNameTag->setMaximumSize(65,35);
    qlbHostNameTag->setMinimumSize(65,35);

    qleHostName->move(60,6);
    qleHostName->setFont(ft);
    qleHostName->setMaximumSize(200,35);
    qleHostName->setMinimumSize(200,35);

    qlbHostPortTag->move(270,6);
    qlbHostPortTag->setText("Port");
    qlbHostPortTag->setFont(ft);
    qlbHostPortTag->setMaximumSize(65,35);
    qlbHostPortTag->setMinimumSize(65,35);

    qleHostPort->move(324,6);
    qleHostPort->setFont(ft);
    qleHostPort->setMaximumSize(60,35);
    qleHostPort->setMinimumSize(60,35);

    btnConnect->move(390,6);
    btnConnect->setText("Connect");
    btnConnect->setFont(ft);
    btnConnect->setMaximumSize(120,35);
    btnConnect->setMinimumSize(120,35);

    qlbPubLishTopicTag->move(6,50);
    qlbPubLishTopicTag->setText("PublishTopic");
    qlbPubLishTopicTag->setFont(ft);
    qlbPubLishTopicTag->setMaximumSize(130,35);
    qlbPubLishTopicTag->setMinimumSize(130,35);

    qlePublishTopic->move(145,50);
    qlePublishTopic->setFont(ft);
    qlePublishTopic->setMaximumSize(240,35);
    qlePublishTopic->setMinimumSize(240,35);

    btnPublish->move(390,50);
    btnPublish->setText("Publish");
    btnPublish->setFont(ft);
    btnPublish->setMaximumSize(120,35);
    btnPublish->setMinimumSize(120,35);

    qlbPublishMessageTag->move(6,94);
    qlbPublishMessageTag->setText("PublishMessage");
    qlbPublishMessageTag->setFont(ft);
    qlbPublishMessageTag->setMaximumSize(160,35);
    qlbPublishMessageTag->setMinimumSize(160,35);

    qlePublishMessage->move(170,94);
    qlePublishMessage->setFont(ft);
    qlePublishMessage->setMaximumSize(400,35);
    qlePublishMessage->setMinimumSize(400,35);

    connect(btnConnect,SIGNAL(clicked()),this,SLOT(on_btnConnect_clicked()));
    connect(btnPublish,SIGNAL(clicked()),this,SLOT(on_btnPublish_clicked()));
}

QMqttClientTool::~QMqttClientTool()
{

}

void QMqttClientTool::on_btnConnect_clicked()
{
    //未连接服务器则连接
    if (m_client->state() == QMqttClient::Disconnected) {
        btnConnect->setText(tr("Disconnect"));
        m_client->setHostname(qleHostName->text());
        m_client->setPort(qleHostPort->text().toInt());
        qleHostName->setEnabled(false);
        qleHostPort->setEnabled(false);
        m_client->connectToHost();
    } else {//断开连接
        btnConnect->setText(tr("Connect"));
        qleHostName->setEnabled(true);
        qleHostPort->setEnabled(true);
        m_client->disconnectFromHost();
    }
}

void QMqttClientTool::on_btnPublish_clicked()
{

    if (m_client->publish(qlePublishTopic->text(),qlePublishMessage->text().toUtf8()) == -1)
         QMessageBox::critical(this, QLatin1String("Error"), QLatin1String("Could not publish message"));
}
//QMqttClientTool.pro
#-------------------------------------------------
#
# Project created by QtCreator 2018-03-10T16:12:38
#
#-------------------------------------------------

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QMqttClientTool
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        QMqttClientTool.cpp

HEADERS += \
        QMqttClientTool.h
INCLUDEPATH += ./include

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/ -lQt5Mqtt
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/ -lQt5Mqttd

INCLUDEPATH += $$PWD/include
DEPENDPATH += $$PWD/include

win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/lib/libQt5Mqtt.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/lib/libQt5Mqttd.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/lib/Qt5Mqtt.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/lib/Qt5Mqttd.lib

六:代码下载

点我下载QMqttClientTool

相关文章