mqt / Qmqtt Qt core module problem in thread (Animal project)
-
Hi ,
We want to utilize Qt module mqt for sending data to our main server. mqt worker will be live in a thread. But we couldnt succeded to make it properly work. Here the sources:
MainWindow:
#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_HWorker.cpp:
#include "worker.h" Worker::Worker(QObject *parent) : QObject(parent) { m_client = new QMqttClient(this); m_client->setHostname("ps01.sample..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_Hit gives below error and we cant send message:
"tate Disconnected 01
QObject::connect: Cannot queue arguments of type 'ClientState' (Make sure 'ClientState' is registered using qRegisterMetaType().) "We want to use signal slot mechanism to send receive messages from Worker thread. This project related to the stray animals/ help needed souls. doing it for charity and stuck here.
Any example for Qt module mqt which is living in a thread and communicating with the main is very hellpful.
I saw and succesfully run the example in qtmqtt , but in thread I couldnt.
thanks
PS: Related with the : helping animals
Maria -
Hi
"QObject::connect: Cannot queue arguments of type 'ClientState'"
It says, for type ClientState, i dont know how to make copy of it. (queue it)Normally you can fix it by simply using qRegisterMetaType under the class definition.
http://doc.qt.io/qt-5/qmetatype.htmlBut i wonder. is that your class ?