QT5.9移植MQTT,QT for Android移植MQTT

MQTT移植源码包下载https://github.com/qt/qtmqtt

一、QT上移植

1、使用qt打开源码包

使用Release套件进行编译,编译后进行移植

2、将源码包的qtmqtt-5.12.8\src\mqtt文件下的所有头文件复制到include内创建的Mqtt文件

3、

4、

将这两个个文件复制到

5、

将该文件添加到

二、QT for Android上移植

1、使用qt打开源码包

使用android套件进行编译,编译后进行移植(虽然警报,但是不影响)

2、将源码包的qtmqtt-5.12.8\src\mqtt文件下的所有头文件复制到include内创建的Mqtt文件

3、

这三个复制到

4、

将该文件移到

三、使用mqtt编写代码,进行实验

1、在.pro中添加

2、相关代码

.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDateTime>
#include <mqtt/qmqttclient.h>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    QMqttClient *m_client = nullptr;

    void MyMQTTSubscribe(QString);
    void MyMQTTSendMessage(const QString, const QString);
signals:

    public slots:
    void brokerConnected();
    void updateLogStateChange();
    void brokerDisconnected();
    void receiveMess(const QByteArray &, const QMqttTopicName &);

private slots:
    void on_connect_btn_clicked();

    void on_sub_btn_clicked();

    void on_send_btn_clicked();

private:
    Ui::MainWindow *ui;
    bool connect_flag=false;
};
#endif // MAINWINDOW_H

.c

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//订阅
void MainWindow::MyMQTTSubscribe(QString str)
{
    auto subscription = m_client->subscribe(str, 0);
    if (!subscription) {
        qDebug() << "Could not subscribe. Is there a valid connection?";
        return;
    }
}

//时间
void MainWindow::updateLogStateChange()
{
    const QString content = QDateTime::currentDateTime().toString()
                    + QLatin1String(": State Change")
                    + QString::number(m_client->state())
                    + QLatin1Char('\n');
    qDebug() << content;
}

//连接
void MainWindow::brokerConnected()
{
    qDebug() << "Connected!";
    QString text=QString{"连接成功,ip=%1,port=%2"}.arg(ui->ip_lineEdit->text()).arg(ui->port_lineEdit->text());
    ui->connect_label->setText(text);
    connect_flag=true;
    if(m_client->state() == QMqttClient::Connected){
        //连接接收消息
        connect(m_client, SIGNAL(messageReceived(QByteArray,QMqttTopicName)), this, SLOT(receiveMess(QByteArray,QMqttTopicName)));
    }
}

//断开
void MainWindow::brokerDisconnected()
{
    qDebug() << "Disconnected!";
}

//接收
void MainWindow::receiveMess(const QByteArray &message, const QMqttTopicName &topic)
{
   QString content;
   content = QDateTime::currentDateTime().toString() + QLatin1Char('\n');
   content += QLatin1String(" Received Topic: ") + topic.name() + QLatin1Char('\n');
   content += QLatin1String(" Message: ") + message + QLatin1Char('\n');
   qDebug() << content;
   ui->recv_textEdit->append(content);
}

//发送
void MainWindow::MyMQTTSendMessage(const QString topic, const QString message)
{
    if (m_client->publish(topic, message.toUtf8()) == -1){
        qDebug() << "Could not publish message";
    }
}

void MainWindow::on_connect_btn_clicked()
{
    QString ip=ui->ip_lineEdit->text();
    QString port=ui->port_lineEdit->text();
    if(ip=="" || port=="")
    {
        qDebug()<<"ip或port内容为空";
        return ;
    }
    m_client = new QMqttClient(this);
    m_client->setHostname(ip);
    m_client->setPort(port.toInt());
    m_client->connectToHost();

//    QTimer *time1=new QTimer(this);
//    time1->setInterval(5000);
//    time1->start();
//    connect(time1,&QTimer::timeout,this,[=](){
//        qDebug()<<1;
//        MyMQTTSendMessage("mcu_test","hello");
//    });

    connect(m_client, &QMqttClient::connected, this, &MainWindow::brokerConnected);
    connect(m_client, &QMqttClient::stateChanged, this, &MainWindow::updateLogStateChange);
    connect(m_client, &QMqttClient::disconnected, this, &MainWindow::brokerDisconnected);

    connect(m_client, &QMqttClient::pingResponseReceived, this, [=]() {
        const QString content = QDateTime::currentDateTime().toString()
                    + QLatin1String(" PingResponse")
                    + QLatin1Char('\n');
        qDebug() << content;
    });
}

void MainWindow::on_sub_btn_clicked()
{
    if(connect_flag)
    {
        QString sub=ui->sub_lineEdit->text();
        if(sub=="")
        {
            qDebug()<<"订阅内容";
            return ;
        }
        m_client->subscribe(sub, 0);//订阅
        //MyMQTTSubscribe("topic2");//订阅
    }
    else
        qDebug()<<"未连接";
}

void MainWindow::on_send_btn_clicked()
{
    if(connect_flag)
    {
        QString topic=ui->topic_lineEdit->text();
        QString text=ui->text_lineEdit->text();
        if(topic=="" || text=="")
        {
            qDebug()<<"主题或内容为空";
            return ;
        }
        else
        {
            MyMQTTSendMessage(topic,text);
        }
    else
        qDebug()<<"未连接";
}

3、效果如图

window端

android端

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值