zoukankan      html  css  js  c++  java
  • Qt

    简介

    使用Qt官方提供的QMQTT源代码编译为QMQTT模块,来编写简单的订阅者和发布者。

    :随后的文章里面会增加怎么实现QMQTT模块的编译和使用。

    订阅者

     1 #ifndef SUBSCRIBER_H
     2 #define SUBSCRIBER_H
     3 
     4 #include <QObject>
     5 
     6 #include <QDebug>
     7 #include <qmqtt.h>
     8 #include <QTimer>
     9 
    10 
    11 //订阅
    12 class Subscriber : public QMQTT::Client {
    13     Q_OBJECT
    14   public:
    15     explicit Subscriber(const QHostAddress& host,
    16                         const quint16 port,
    17                         QObject* parent = NULL)
    18         : QMQTT::Client(host, port, parent)
    19         , _qout(stdout) {
    20         connect(this, &Subscriber::connected, this, &Subscriber::onConnected);
    21         connect(this, &Subscriber::subscribed, this, &Subscriber::onSubscribed);
    22         connect(this, &Subscriber::received, this, &Subscriber::onReceived);
    23     }
    24     virtual ~Subscriber() {}
    25 
    26     QTextStream _qout;
    27 
    28   public slots:
    29     void onConnected() {
    30         qDebug() << "connected" << endl;
    31         subscribe("qmqtt/exampletopic", 0);
    32     }
    33 
    34     void onSubscribed(const QString& topic) {
    35         qDebug() << "subscribed " << topic << endl;
    36     }
    37 
    38     void onReceived(const QMQTT::Message& message) {
    39         qDebug() << "publish received: ""
    40                  << QString::fromUtf8(message.payload())
    41                  << """ << endl;
    42     }
    43 };
    44 
    45 
    46 #endif // SUBSCRIBER_H

    发布者

     1 #ifndef PUBLISHER_H
     2 #define PUBLISHER_H
     3 
     4 #include <QObject>
     5 
     6 #include <QDebug>
     7 #include <qmqtt.h>
     8 #include <QTimer>
     9 
    10 //发布
    11 class Publisher : public QMQTT::Client {
    12     Q_OBJECT
    13   public:
    14     explicit Publisher(const QHostAddress& host,
    15                        const quint16 port,
    16                        QObject* parent = NULL)
    17         : QMQTT::Client(host, port, parent), _number(0) {
    18 
    19         connect(this, &Publisher::connected, this, &Publisher::onConnected);
    20         connect(&_timer, &QTimer::timeout, this, &Publisher::onTimeout);
    21         connect(this, &Publisher::disconnected, this, &Publisher::onDisconnected);
    22     }
    23     virtual ~Publisher() {}
    24 
    25     QTimer _timer;
    26     quint16 _number;
    27 
    28   public slots:
    29     void onConnected() {
    30         subscribe("qmqtt/exampletopic", 0);
    31         _timer.start(1000);
    32     }
    33 
    34     void onTimeout() {
    35         QMQTT::Message message(_number, "qmqtt/exampletopic",
    36                                QString("Number is %1").arg(_number).toUtf8());
    37         publish(message);
    38         _number++;
    39 //        if(_number >= 10) {
    40 //            _timer.stop();
    41 //            disconnectFromHost();
    42 //        }
    43     }
    44 
    45     void onDisconnected() {}
    46 };
    47 
    48 #endif // PUBLISHER_H
    博客园文作者:Citrusliu 博文地址:https://www.cnblogs.com/citrus
  • 相关阅读:
    牛客寒假算法基础集训营1 C 小a与星际探索(思维+异或)
    牛客寒假算法基础集训营4 E Applese 涂颜色
    Python的range()函数用法
    R语言读取XML数据
    R语言如何读取.csv文件
    反转链表
    HashSet HashTable HashMap 区别
    输入两个整数 n 和 m,从数列1,2,3.......n 中随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来.
    Activity生命周期之我见
    左旋转字符串
  • 原文地址:https://www.cnblogs.com/citrus/p/14971440.html
Copyright © 2011-2022 走看看