源码下载
https://github.com/qt/qtmqtt/tags?after=v6.0.0
编译过程
将源码解压,进入源码路径:
cd qtmqtt-5.15.2\src\mqtt\
mkdir D:\Qt\5.12.12\mingw73_64\QtMqtt
xcopy *.h D:\Qt\5.12.12\mingw73_64\QtMqtt
qmake
mingw32-make
编译中,会出现错误1:
qmqttconnection.cpp: In member function 'bool QMqttConnection::ensureTransport(bool)':
qmqttconnection.cpp:169:39: error: 'errorOccurred' is not a member of 'QAbstractSocket'
connect(socket, &QAbstractSocket::errorOccurred, this, &QMqttConnection::transportError);
^~~~~~~~~~~~~
这是Qt5.12.12的QAbstractSocket类中还没有相应的对象,修改响应的代码行为:
connect(socket, &QAbstractSocket::connected, this, &QMqttConnection::transportConnectionEstablished);
connect(socket, &QAbstractSocket::disconnected, this, &QMqttConnection::transportConnectionClosed);
// connect(socket, &QAbstractSocket::errorOccurred, this, &QMqttConnection::transportError);
connect(socket, static_cast<void (QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error),
this, &QMqttConnection::transportError);
再次编译,会出现:
qmqtttopicname.cpp: In member function 'QStringList QMqttTopicName::levels() const':
qmqtttopicname.cpp:148:48: error: 'KeepEmptyParts' is not a member of 'Qt'
return d->name.split(QLatin1Char('/'), Qt::KeepEmptyParts);
^~~~~~~~~~~~~~
修改相应的行为
QStringList QMqttTopicName::levels() const
{
return d->name.split(QLatin1Char('/'), QString::KeepEmptyParts);
}
再次编译,通过,继续之后后续动作完成源码安装:
mingw32-make install
编译总结
- 下载并解压源码
- 在Qt工具链相应include目录下创建QtMqtt目录
- 将源码src/*.h拷贝到上述目录中
- 修改qmqttconnection.cpp文件169行,修改内容如上
- 修改qmqtttopicname.cpp文件148行,修改内容如上
- 执行以下脚本
cd qtmqtt-5.15.2\src\mqtt\
mkdir D:\Qt\5.12.12\mingw73_64\QtMqtt
xcopy *.h D:\Qt\5.12.12\mingw73_64\QtMqtt
# 修改代码
qmake
mingw32-make
mingw32-make install
用法
- 在pro文件中包含 mqtt:QT+=mqtt
- 在源码中包含头文件: #include <qmqttclient.h>