qt的mqtt编译及使用方法

1 篇文章 0 订阅

在qt5.10之前,mqtt需要自己编译库,

1.源码路径

下载地址:https://github.com/emqx/qmqtt,注意要下载qmqtt-master版本的,不要下载qtmqtt-dev

 

2.生成库文件

使用qtcreator打开qmqtt.pro文件,构建src工程,

编译报错:Could not create directory "...\qmqtt\Error in " Util.asciify("build-qmqtt-Qt_5_6_3_msvc2013-Debug")": TypeError: Property 'asciify' of object Core::Internal::UtilsJsExtension(0x34bb080) is not a function\src"

Error while building/deploying project qmqtt (kit: Qt 5.6.3 (msvc2013))

When executing step "qmake"

解决方法:

左边树,选择项目,将影子构建取消勾选。

 

再次构建src工程,可生成lib文件夹,里面含debug和Release版本的dll和lib文件。

Qt5Qmqtt.dll、Qt5Qmqtt.lib、Qt5Qmqttd.dll、Qt5Qmqttd.lib

3.文件拷贝

将文件拷贝到qt的安装目录指定文件夹下,这样就当作了qt自己的文件来使用了。

3.1.h文件拷贝

在qt的安装目录include文件夹下(如D:\Qt\Qt5.6.3_2013\5.6.3\msvc2013\include),创建文件夹QtMqtt,并将文件夹qmqtt-master\src\mqtt下的.h文件拷贝到QtMqtt文件夹下。

​​​​​​​3.2Lib和dll文件拷贝

Lib文件:将Qt5Qmqtt.lib和Qt5Qmqttd.lib文件拷贝到qt的安装目录的lib文件夹下(如D:\Qt\Qt5.6.3_2013\5.6.3\msvc2013\lib)。

Dll文件:将Qt5Qmqtt.dll和Qt5Qmqttd.dll文件拷贝到qt的安装目录的bin文件夹下(如D:\Qt\Qt5.6.3_2013\5.6.3\msvc2013\bin)。

3.3调用方法

在vs2013工程--右键--属性,C/C++--常规--附加包含目录,添加$(QTDIR)\include\QtMqtt;链接器--输入--附加依赖项,添加$(QTDIR)\lib\Qt5Qmqttd.lib(Release为$(QTDIR)\lib\Qt5Qmqtt.lib)。在使用时,#include <qmqtt.h>即可。

4.Demo演示

关键方法:

m_client = new QMQTT::Client(QHostAddress(ip), port, this);

m_client->setUsername(Username);

m_client->setPassword(Password);

m_client->setClientId(clientid);

m_client->connectToHost();

 

//m_client->setAutoReconnect(true);

 

connect(m_client, &QMQTT::Client::connected, this, [this]() {

const QString content = QLatin1String(" connected");

});

connect(m_client, &QMQTT::Client::disconnected, this, [this]() {

const QString content = QLatin1String(" disconnected");

});

 

connect(m_client, &QMQTT::Client::received, this, [this](const QMQTT::Message& message) {

const QString content = QString("received, Topic: %1, Message: %2").arg(message.topic()).arg(QString(message.payload()));

    });

 

connect(m_client, &QMQTT::Client::pingresp, this, [this]() {

        const QString content = QLatin1String(" pingresp");

    });

connect(m_client, &QMQTT::Client::subscribed, this, [this](const QString& topic, const quint8 qos) {

const QString content = QString("subscribed, Topic: %1, qos: %2").arg(topic).arg(qos);

});

connect(m_client, &QMQTT::Client::unsubscribed, this, [this](const QString& topic) {

const QString content = QString("unsubscribed, Topic: %1").arg(topic);

});

connect(m_client, &QMQTT::Client::published, this, [this](const QMQTT::Message& message, quint16 msgid) {

const QString content = QString("published, Topic: %1, Message: %2, msgid:%3").arg(message.topic()).arg(QString(message.payload())).arg(msgid);

});

connect(m_client, &QMQTT::Client::error, this, [this](const QMQTT::ClientError error) {

const QString content = QString("error, value: %1").arg(error);

});

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Building on: linux-g++ (x86_64, CPU features: mmx sse sse2) Building for: linux-aarch64-gnu-g++ (arm64, CPU features: neon) Target compiler: gcc 6.3.1 Configuration: cross_compile use_gold_linker compile_examples enable_new_dtags largefile neon precompile_header shared rpath release c++11 c++14 concurrent dbus reduce_exports stl Build options: Mode ................................... release Optimize release build for size ........ no Building shared libraries .............. yes Using C standard ....................... C11 Using C++ standard ..................... C++14 Using ccache ........................... no Using gold linker ...................... yes Using new DTAGS ........................ yes Using precompiled headers .............. yes Using LTCG ............................. no Target compiler supports: NEON ................................. yes Build parts ............................ libs Qt modules and options: Qt Concurrent .......................... yes Qt D-Bus ............................... yes Qt D-Bus directly linked to libdbus .... no Qt Gui ................................. yes Qt Network ............................. yes Qt Sql ................................. yes Qt Testlib ............................. yes Qt Widgets ............................. yes Qt Xml ................................. yes Support enabled for: Using pkg-config ....................... yes udev ................................... no Using system zlib ...................... yes Qt Core: DoubleConversion ....................... yes Using system DoubleConversion ........ no GLib ................................... no iconv .................................. yes ICU .................................... no Tracing backend ........................ Logging backends: journald ............................. no syslog ............................... no slog2 ................................ no Using system PCRE2 ..................... no Qt Network: getifaddrs() ........................... yes IPv6 ifname ............................ yes libproxy ............................... no Linux AF_NETLINK ....................... yes OpenSSL ................................ yes Qt directly linked to OpenSSL ........ no OpenSSL 1.1 ............................ no DTLS ................................... yes SCTP ................................... no Use system proxies ..................... yes Qt Gui: Accessibility .......................... yes FreeType ............................... yes Using system FreeType ................ no HarfBuzz ............................... yes Using system HarfBuzz ................ no Fontconfig ............................. no Image formats: GIF .................................. yes ICO .................................. yes JPEG ................................. yes Using system libjpeg ............... yes PNG .................................. yes Using system libpng ................ no EGL .................................... no OpenVG ................................. no OpenGL: Desktop OpenGL ....................... no OpenGL ES 2.0 ........................ no OpenGL ES 3.0 ........................ no OpenGL ES 3.1 ........................ no OpenGL ES 3.2 ........................ no Vulkan ................................. no Session Management ..................... yes Features used by QPA backends: evdev .................................. yes libinput ............................... no INTEGRITY HID .......................... no mtdev .................................. no tslib .................................. no xkbcommon .............................. no X11 specific: XLib ................................. no EGL on X11 ........................... no QPA backends: DirectFB ............................... no EGLFS .................................. no LinuxFB ................................ yes VNC .................................... yes Mir client ............................. no Qt Sql: SQL item models ........................ yes Qt Widgets: GTK+ ................................... no Styles ................................. Fusion Windows Qt PrintSupport: CUPS ................................... no Qt Sql Drivers: DB2 (IBM) .............................. no InterBase .............................. no MySql .................................. no OCI (Oracle) ........................... no ODBC ................................... no PostgreSQL ............................. no SQLite2 ................................ no SQLite ................................. yes Using system provided SQLite ......... no TDS (Sybase) ........................... no Qt Testlib: Tester for item models ................. yes Qt SerialBus: Socket CAN ............................. yes Socket CAN FD .......................... yes Qt QML: QML network support .................... yes QML debugging and profiling support .... yes QML sequence object .................... yes QML list model ......................... yes QML XML http request ................... yes QML Locale ............................. yes QML delegate model ..................... yes Qt Quick: Direct3D 12 ............................ no AnimatedImage item ..................... yes Canvas item ............................ yes Support for Qt Quick Designer .......... yes Flipable item .......................... yes GridView item .......................... yes ListView item .......................... yes TableView item ......................... yes Path support ........................... yes PathView item .......................... yes Positioner items ....................... yes Repeater item .......................... yes ShaderEffect item ...................... yes Sprite item ............................ yes Qt Scxml: ECMAScript data model for QtScxml ...... yes Qt Gamepad: SDL2 ................................... no Qt 3D: Assimp ................................. yes System Assimp .......................... no Output Qt3D Job traces ................. no Output Qt3D GL traces .................. no Use SSE2 instructions .................. no Use AVX2 instructions .................. no Aspects: Render aspect ........................ yes Input aspect ......................... yes Logic aspect ......................... yes Animation aspect ..................... yes Extras aspect ........................ yes Qt 3D Renderers: OpenGL Renderer ........................ yes Qt 3D GeometryLoaders: Autodesk FBX ........................... no Qt Wayland Client ........................ no Qt Wayland Compositor .................... no Qt Bluetooth: BlueZ .................................. no BlueZ Low Energy ....................... no Linux Crypto API ....................... no WinRT Bluetooth API (desktop & UWP) .... no Qt Sensors: sensorfw ............................... no Qt Quick Controls 2: Styles ................................. Default Fusion Imagine Material Universal Qt Quick Templates 2: Hover support .......................... yes Multi-touch support .................... yes Qt Positioning: Gypsy GPS Daemon ....................... no WinRT Geolocation API .................. no Qt Location: Qt.labs.location experimental QML plugin . yes Geoservice plugins: OpenStreetMap ........................ yes HERE ................................. yes Esri ................................. yes Mapbox ............................... yes MapboxGL ............................. no Itemsoverlay ......................... yes QtXmlPatterns: XML schema support ..................... yes Qt Multimedia: ALSA ................................... no GStreamer 1.0 .......................... no GStreamer 0.10 ......................... no Video for Linux ........................ yes OpenAL ................................. no PulseAudio ............................. no Resource Policy (libresourceqt5) ....... no Windows Audio Services ................. no DirectShow ............................. no Windows Media Foundation ............... no Qt Tools: QDoc ................................... no Qt WebEngine: Embedded build ......................... yes Pepper Plugins ......................... no Printing and PDF ....................... no Proprietary Codecs ..................... no Spellchecker ........................... yes Native Spellchecker .................... no WebRTC ................................. no Use System Ninja ....................... no Geolocation ............................ yes WebChannel support ..................... yes Use v8 snapshot ........................ yes Kerberos Authentication ................ no Building v8 snapshot supported ......... yes Use ALSA ............................... no Use PulseAudio ......................... no Optional system libraries used: re2 .................................. no icu .................................. no libwebp, libwebpmux and libwebpdemux . no opus ................................. no ffmpeg ............................... no libvpx ............................... no snappy ............................... no glib ................................. no zlib ................................. yes minizip .............................. no libevent ............................. no jsoncpp .............................. no protobuf ............................. no libxml2 and libxslt .................. no lcms2 ................................ no png .................................. no JPEG ................................. no harfbuzz ............................. no freetype ............................. no x11 .................................. no Required system libraries: fontconfig ........................... no dbus ................................. no nss .................................. no khr .................................. no glibc ................................ yes Required system libraries for qpa-xcb: libdrm ............................... no xcomposite ........................... no xcursor .............................. no xi ................................... no xrandr ............................... no xtst ................................. no Note: Also available for Linux: linux-clang linux-icc
### 回答1: 在Linux/ARM下编译安装Qt MQTT库,您可以按照以下步骤进行操作: 1. 首先,确保您已经在ARM架构的Linux系统上安装了必要的编译工具和依赖项,例如GCC、make等。可以通过包管理器或从源代码手动安装它们。 2. 下载Qt MQTT库的源代码,可以从Qt官方网站或其他可靠的资源下载。确保选择与您的Qt版本兼容的源代码。 3. 解压源代码文件,并进入解压后的目录。 4. 打开终端,进入源代码目录,并执行以下命令来生成Makefile: ``` qmake ``` 5. 接下来,使用make命令编译库文件: ``` make ``` 6. 编译过程可能会需要一些时间,具体取决于您的系统性能。请耐心等待编译完成。 7. 完成编译后,可以使用以下命令安装Qt MQTT库到系统中: ``` make install ``` 8. 安装完成后,您可以在系统的Qt库目录中找到MQTT库文件。可以通过以下命令获取库文件的位置: ``` qmake -query QT_INSTALL_LIBS ``` 9. 接下来,您可以在Qt项目中使用MQTT库。在项目的.pro文件中添加以下行: ``` LIBS += -lQt5Mqtt ``` 以上就是在Linux/ARM下编译安装Qt MQTT库的简要步骤。请注意,这只是一般的指导,具体步骤可能会因您所使用的系统和版本而有所差异。请参考Qt官方文档或相关资源获取更详细的信息。 ### 回答2: 在Linux/ARM平台下,编译安装Qt MQTT库的过程如下: 1. 首先,确保系统中已经安装了必要的开发工具链和依赖库。可以使用以下命令安装: ``` sudo apt-get update sudo apt-get install build-essential sudo apt-get install libssl-dev ``` 2. 下载Qt MQTT库的源代码。可以从Qt官方网站的下载页面或Github上的项目页面获取最新的源代码。 3. 解压下载的源代码。可以使用以下命令: ``` tar -xzvf qt-mqtt-xxx.tar.gz cd qt-mqtt-xxx ``` 4. 创建一个用于编译的目录,并进入该目录: ``` mkdir build cd build ``` 5. 配置编译选项。运行以下命令,选择所需的配置选项: ``` qmake .. ``` 你可以在此处添加其他自定义的配置选项。 6. 运行make命令进行编译: ``` make ``` 这将会生成Qt MQTT库的可执行文件。 7. (可选)运行make install命令进行安装。这将会将Qt MQTT库复制到系统目录中,以便其他项目可以使用: ``` sudo make install ``` 如果不想全局安装库,可以跳过此步骤,将生成的库文件手动复制到项目中使用。 完成上述步骤后,你就成功地在Linux/ARM平台下编译安装了Qt MQTT库。注意,具体的命令和选项可能会根据你所使用的Linux发行版和版本而有所不同,以上命令仅供参考。 ### 回答3: 在Linux/ARM下编译安装Qt MQTT库,需要按照以下步骤进行: 1. 首先,确保你已经安装了Qt开发环境和所需的编译工具链。你可以使用命令行工具或者图形界面工具进行安装。 2. 下载Qt MQTT库的源代码。你可以在Qt的官方网站上找到对应的版本。下载后,解压缩源代码到你的开发环境下的任意目录。 3. 打开终端,进入解压缩后的源代码目录。使用cd命令进行切换目录。 4. 创建一个新的构建目录,并进入该目录。这个目录将用于存储编译生成的文件。可以使用以下命令创建并进入该目录: ``` mkdir build cd build ``` 5. 使用qmake命令生成Makefile文件。执行以下命令: ``` qmake .. ``` 6. 使用make命令进行编译。执行以下命令: ``` make ``` 7. 编译完成后,可以使用make install命令进行安装。执行以下命令: ``` make install ``` 8. 安装完成后,你可以在系统的Qt库目录中找到编译生成的MQTT库文件。其中包括头文件和库文件。 9. 最后,你可以在你的项目中使用Qt MQTT库。在你的项目文件中添加相应的包含路径和链接库即可开始使用。 请注意,以上步骤中涉及到的命令和路径可能需要根据你的具体环境和需求进行调整。此外,确保你已经阅读并理解了Qt MQTT库的编译要求和依赖项,以确保顺利进行编译和安装。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值