环境
Qt 5.14.2
qtmqtt
mqttx
功能
QT Mqtt客户端
qtmqtt
- 下载 qtmqtt (注意下载与QT版本相符的库)并使用QT 编译
编译完成后需要的文件:
emqx
1.虚拟机中安装emqx,并启动
curl -s https://assets.emqx.com/scripts/install-emqx-deb.sh | sudo bash
sudo apt-get install emqx
sudo systemctl start emqx
2.访问emqx
用户:admin,密码public
http://IPAddress:18083/
源码
1.引入库文件及H文件
win32: LIBS += -L$$PWD/./ -lQt5MqttINCLUDEPATH += $$PWD/.
DEPENDPATH += $$PWD/.INCLUDEPATH += $$PWD/Qt5Mqtt/
2.mainwindow.h
#include "Qt5Mqtt/qmqttclient.h"
class MainWindow : public QMainWindow
{Q_OBJECTpublic slots:void setClientPort(int p);
private slots:void on_buttonConnect_clicked();void updateLogStateChange();void brokerDisconnected();void on_buttonPublish_clicked();void on_buttonSubscribe_clicked();
private:Ui::MainWindow *ui;QMqttClient *m_client;
};
3.mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);m_client = new QMqttClient(this);m_client->setUsername("user");m_client->setPassword("P@ssw0rd");m_client->setHostname(ui->lineEditHost->text());m_client->setPort(ui->spinBoxPort->value());connect(m_client, &QMqttClient::stateChanged, this, &MainWindow::updateLogStateChange);connect(m_client, &QMqttClient::disconnected, this, &MainWindow::brokerDisconnected);connect(m_client, &QMqttClient::messageReceived, this, [this](const QByteArray &message, const QMqttTopicName &topic) {const QString content = QDateTime::currentDateTime().toString()+ QLatin1String(" Received Topic: ")+ topic.name()+ QLatin1String(" Message: ")+ message+ QLatin1Char('\n');ui->editLog->insertPlainText(content);});connect(m_client, &QMqttClient::pingResponseReceived, this, [this]() {const QString content = QDateTime::currentDateTime().toString()+ QLatin1String(" PingResponse")+ QLatin1Char('\n');ui->editLog->insertPlainText(content);});connect(ui->lineEditHost, &QLineEdit::textChanged, m_client, &QMqttClient::setHostname);connect(ui->spinBoxPort, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::setClientPort);updateLogStateChange();
}void MainWindow::on_buttonConnect_clicked()
{if (m_client->state() == QMqttClient::Disconnected){m_client->connectToHost();} else {m_client->disconnectFromHost();}
}void MainWindow::updateLogStateChange()
{switch(m_client->state()){case QMqttClient::ClientState::Connected:{ui->lineEditTopic->setEnabled(true);ui->lineEditMessage->setEnabled(true);ui->buttonSubscribe->setEnabled(true);ui->buttonPublish->setEnabled(true);ui->lineEditHost->setEnabled(false);ui->spinBoxPort->setEnabled(false);ui->buttonConnect->setText(tr("Disconnect"));}break;default:{ui->lineEditTopic->setEnabled(false);ui->lineEditMessage->setEnabled(false);ui->buttonSubscribe->setEnabled(false);ui->buttonPublish->setEnabled(false);ui->lineEditHost->setEnabled(true);ui->spinBoxPort->setEnabled(true);ui->buttonConnect->setText(tr("Connect"));}break;}const QString content = QDateTime::currentDateTime().toString()+ QLatin1String(": State Change")+ QString::number(m_client->state())+ QLatin1Char('\n');ui->editLog->insertPlainText(content);
}void MainWindow::brokerDisconnected()
{ui->lineEditHost->setEnabled(true);ui->spinBoxPort->setEnabled(true);ui->buttonConnect->setText(tr("Connect"));
}void MainWindow::setClientPort(int p)
{m_client->setPort(p);
}void MainWindow::on_buttonPublish_clicked()
{if (m_client->publish(ui->lineEditTopic->text(), ui->lineEditMessage->text().toUtf8()) == -1)QMessageBox::critical(this, QLatin1String("Error"), QLatin1String("Could not publish message"));
}void MainWindow::on_buttonSubscribe_clicked()
{auto subscription = m_client->subscribe(ui->lineEditTopic->text());if (!subscription) {QMessageBox::critical(this, QLatin1String("Error"), QLatin1String("Could not subscribe. Is there a valid connection?"));return;}
}