<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[mqt &#x2F; Qmqtt Qt core module problem in thread (Animal project)]]></title><description><![CDATA[<p dir="auto">Hi ,</p>
<p dir="auto">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:</p>
<p dir="auto">MainWindow:</p>
<pre><code>#include "mainwindow.h"
#include "ui_mainwindow.h"

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

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

//    worker-&gt;connecting();
}

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

</code></pre>
<p dir="auto">mainwindow.h:</p>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QMainWindow&gt;
#include &lt;QThread&gt;
#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

</code></pre>
<p dir="auto">Worker.cpp:</p>
<pre><code>#include "worker.h"

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


    m_client = new QMqttClient(this);
    m_client-&gt;setHostname("ps01.sample..com");
    m_client-&gt;setPort(1883);



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

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





        connect(m_client, &amp;QMqttClient::messageReceived, this, [this](const QByteArray &amp;message, const QMqttTopicName &amp;topic) {
                const QString content = QDateTime::currentDateTime().toString()
                            + QLatin1String(" Received Topic: ")
                            + topic.name()
                            + QLatin1String(" Message: ")
                            + message
                            + QLatin1Char('\n');

                qDebug() &lt;&lt; content &lt;&lt; endl;
            });


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


//            qDebug() &lt;&lt; m_client-&gt;publish(topic, message.toUtf8(), 0, false) &lt;&lt; "tedest" &lt;&lt;endl;

}

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

void Worker::brokerDisconnected()
{

}

void Worker::pingReceived(){


}

void Worker::connecting()
{
    qDebug() &lt;&lt; "connect tohost . . . " &lt;&lt; endl;
    m_client-&gt;connectToHost();

}

void Worker::sendingMessage()
{


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



}



</code></pre>
<p dir="auto">worker.h :</p>
<pre><code>#ifndef WORKER_H
#define WORKER_H

#include &lt;QObject&gt;
#include &lt;QtMqtt&gt;
#include &lt;QTcpSocket&gt;
#include &lt;QtMqtt/QMqttClient&gt;

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

</code></pre>
<p dir="auto">it gives below error and we cant send message:</p>
<p dir="auto">"tate Disconnected 01</p>
<pre><code>QObject::connect: Cannot queue arguments of type 'ClientState'
(Make sure 'ClientState' is registered using qRegisterMetaType().) "
</code></pre>
<p dir="auto">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.</p>
<p dir="auto">Any example for Qt module mqt which is living in a thread and communicating with the main is very hellpful.</p>
<p dir="auto">I saw and succesfully run the example in qtmqtt , but in thread I couldnt.</p>
<p dir="auto">thanks</p>
<p dir="auto">PS: Related with the : <a href="https://forum.qt.io/topic/89316/messaging-choice-for-both-binary-text-payload-mqtt-amqp-zeromq-etc">helping animals</a><br />
Maria</p>
]]></description><link>https://forum.qt.io/topic/89368/mqt-qmqtt-qt-core-module-problem-in-thread-animal-project</link><generator>RSS for Node</generator><lastBuildDate>Tue, 12 May 2026 04:21:55 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/89368.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 29 Mar 2018 16:39:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to mqt &#x2F; Qmqtt Qt core module problem in thread (Animal project) on Thu, 29 Mar 2018 17:53:57 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
"QObject::connect: Cannot queue arguments of type 'ClientState'"<br />
It says, for type ClientState, i dont know how to make copy of it. (queue it)</p>
<p dir="auto">Normally you can fix it by simply using qRegisterMetaType under the class definition.<br />
<a href="http://doc.qt.io/qt-5/qmetatype.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qmetatype.html</a></p>
<p dir="auto">But i wonder. is that your class ?</p>
]]></description><link>https://forum.qt.io/post/450314</link><guid isPermaLink="true">https://forum.qt.io/post/450314</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Thu, 29 Mar 2018 17:53:57 GMT</pubDate></item></channel></rss>