Qt development MQTT (a) of the official Qt Qt MQTT

Outline

Qt development program MQTT There are two ways, one is based on the official Qt MQTT package, a third party (EMQ) developed calling for Qt MQTT interface, both using similar methods, and each source is provided. So, where the first to introduce a first, Qt based packages as provided by the official use MQTT.

Although the official Qt in 2017 has provided a package of MQTT, but has not officially added to the standard library Qt inside, you need to download the source code to compile their own.

Qt official document description Address:https://doc.qt.io/QtMQTT/qtmqtt-index.html

download

Official Qt source code on github, address: https://github.com/qt/qtmqtt
Here Insert Picture Description
This is the latest, based on MQTT 5.0 protocol version. First download the source code.

Compile

Get the following files download source code, compiled directly open the project file ready
Here Insert Picture Description
I am here to build environment is: Qt5.12.3 + vs2017

Note that the compiler source code need to install this perl, otherwise it will error:perl 不是内部或外部命令,也不是可运行的程序。

perl Download: https: //www.perl.org/get.html
network download speed is slower officer, I uploaded to a network disk, can be downloaded here:

Link: https: //pan.baidu.com/s/1p5YOo-FU-ZLJUtuZSN0Rjg extraction code: i0dm

After installing automatically write Perl environment variable, this time again compiled (Release mode) QtMqtt source, the following files compiled:
Here Insert Picture Description

Bin directory is what we want library file:
Here Insert Picture Description

Then you can mqtt deployed to own the Qt project.

Deployed to the Qt project

Compiled by the Qt Mqtt library, to use it in two ways, one is to import external libraries and header files directly in the project, there is a will to deploy Qt installation directory in the form of modules, wherein the second the advantage is that only have to do one operation, then later need Mqtt library can be called directly, do not need to import every external libraries. Here are two ways to explain, first of all look at the first.

Importing external libraries

Create a Qt project
and then just compile the source code to generate the lib directory folder copy the following four files:
Here Insert Picture DescriptionHere Insert Picture Description

在新建工程目录下创建lib文件夹,将拷贝的文件粘贴进去:
Here Insert Picture Description
然后在qtmqtt源码目录下(qtmqtt\src\mqtt)的所有.h头文件拷贝,在新建工程目录下创建include文件夹,将拷贝的文件粘贴进去:
Here Insert Picture Description

打开新建工程的pro文件,添加:
Here Insert Picture Description
再添加库文件引用:

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

添加include文件的引用:

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

ok, 这样就可以调用Qt Mqtt的库文件了,直接可以包含头文件来使用了。#include "QtMqtt/QMqttClient"

为了统一演示,先介绍完第二种方法后再来看demo。

部署到Qt安装目录

再来看第二种方法,如何将QtMqtt的库直接部署到Qt安装目录中,这样只需要部署一次,以后在任何工程中引用都不需要再额外导入库了,相比第一种来说更方便些。

首先,将qtmqtt源码目录下(qtmqtt\src\mqtt)的所有.h头文件拷贝,Qt安装目录下的include文件夹中创建一个mqtt目录,将拷贝的文件粘贴进去:
Here Insert Picture DescriptionHere Insert Picture Description
然后,将源码编译生成目录下的lib中以下6个文件拷贝
Here Insert Picture Description
并粘贴到Qt安装目录下的lib文件夹中去:
Here Insert Picture Description

接下来将源码编译生成的两个库文件拷贝到Qt安装目录的bin中:
Here Insert Picture Description
Here Insert Picture Description

最后再拷贝模块配置文件到Qt安装目录中
Here Insert Picture Description
Here Insert Picture Description

ok,配置完毕,这种方式配置在新建工程中引用只需要引入模块就可以直接使用了

QT += mqtt

包含头文件

#include <QtMqtt/QtMqtt>

所以推荐使用第二种方式进行配置。

接下来看看Demo。

Demo演示

为了方便演示,我们直接使用Qt Mqtt源码中自带的示例来编译运行。
Here Insert Picture DescriptionHere Insert Picture Description
这个示例Qt官方有详细介绍的,https://doc.qt.io/QtMQTT/qtmqtt-simpleclient-example.html
将该示例打开,有个地方需要改一下,打开Pro文件
Here Insert Picture Description
将以上两行注释掉,要不然会有依赖,无法独立运行。

我们直接编译运行该示例:
broker.hivemq.com
Here Insert Picture Description
在官方文档中介绍,可以直接连接以下两个服务器地址:
Here Insert Picture Description

After testing, the first address not connected, so we enter the second direct address test: broker.hivemq.com
Here Insert Picture Description
Enter the address, click the link after you can connect to the server, and the State became 2 says have connected.
Then click on the subscribe button to indicate that you want to subscribe to this theme at the click release, you can receive the information:
Here Insert Picture Description
can be tested on more than one computer, as long subscribed to the same theme, it sends a message at one end, the other end can receive the appropriate Information.

The official Qt provides a very detailed document describes , we can refer to use interface, the main use of the class is QMqttClient.

At this point, Qt provides official MQTT package module using the presentation have all been finished, the next will introduce a third party based on how MQTT package.

Published 254 original articles · won praise 484 · views 500 000 +

Guess you like

Origin blog.csdn.net/luoyayun361/article/details/104671603