mqt / Qmqtt Qt核心模块中线程奇怪的行为(来自Animal帮助项目)

时间:2018-03-29 16:18:40

标签: c++ qt mqtt qthread

我们想利用Qt模块mqtt将数据发送到我们的主服务器。 mqtt工作者将活在一个线程中。但我们无法成功地使其正常运作。来源:

主窗口:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    q_thread = new QThread();
    worker = new Worker();
    worker->moveToThread(q_thread);
    q_thread->start();

//    worker->connecting();
}

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

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QThread>
#include "worker.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    QThread *q_thread;
    Worker *worker;


private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

Worker.cpp:

#include "worker.h"

Worker::Worker(QObject *parent) : QObject(parent)
{


    m_client = new QMqttClient(this);
    m_client->setHostname("ps01.test.com");
    m_client->setPort(1883);



    qDebug() << "?? is here"  << m_client->state() << endl;
    if (m_client->state() == QMqttClient::Disconnected){
        qDebug() << "state Disconnected 01" << endl;}
    else{qDebug() << "state Connected 01" << endl;};

        m_client->connectToHost();
        connect(m_client, &QMqttClient::stateChanged, this, &Worker::updateLogStateChange);
        connect(m_client, &QMqttClient::disconnected, this, &Worker::brokerDisconnected);
        connect(m_client, &QMqttClient::connected, this, &Worker::pingReceived);





        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');

                qDebug() << content << endl;
            });


        connect(m_client, &QMqttClient::pingResponseReceived, this , &Worker::pingReceived);


//            qDebug() << m_client->publish(topic, message.toUtf8(), 0, false) << "tedest" <<endl;

}

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

void Worker::brokerDisconnected()
{

}

void Worker::pingReceived(){


}

void Worker::connecting()
{
    qDebug() << "connect tohost . . . " << endl;
    m_client->connectToHost();

}

void Worker::sendingMessage()
{


    if (m_client->publish(topic,
                                      message.toUtf8(),
                                      1,
                                      true) == -1)
                    qDebug() << " can not publish message" << endl;



}

worker.h:

#ifndef WORKER_H
#define WORKER_H

#include <QObject>
#include <QtMqtt>
#include <QTcpSocket>
#include <QtMqtt/QMqttClient>

class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(QObject *parent = nullptr);

private:

   QMqttClient  *m_client;
   const QString topic = "qtmqtt/topic1";
   const QString message =".....Alpullu 900909";


signals:

public slots:
   void updateLogStateChange();
   void brokerDisconnected();
   void pingReceived();
   void connecting();
   void sendingMessage();
};

#endif // WORKER_H

它给出了以下错误,我们无法发送消息:

“状态断开01

QObject::connect: Cannot queue arguments of type 'ClientState'
(Make sure 'ClientState' is registered using qRegisterMetaType().) "

我们希望使用信号槽机制从Worker线程发送接收消息。这个项目涉及流浪动物/帮助需要的灵魂。为慈善事业而努力并坚持到这里。

Qt模块mqt的任何示例都存在于一个线程中并与main进行通信非常有帮助。

我在qtmqtt中看到并成功运行了该示例,但在线程中我无法做到。

PS:与helping animals

相关

0 个答案:

没有答案