Signals not working anymore after QObjects being moved into QThreads

Signals not working anymore after QObjects being moved into QThreads



Basically, the title... if there is no QThread (or it is just commented) I get the following result:


LOG> Log working!
LOG> PRODUCER: sent resource address: 29980624
PRODUCER: sent resource address: 29980624
CONSUMER: received resource address: 29980624



29980624, or any relevant memory position.



But, when un-commented just


LOG> Log working!



mainwindow.h


class MainWindow : public QMainWindow

Q_OBJECT

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

public slots:
void slot_log(QString str);

signals:
void signal_log(QString str);

private:
void createConsumer( void );
void deleteConsumer( void );
void createProducer( void );
void deleteProducer( void );
void createConnections( void );
SingleConsumer *consumer;
QThread *thread_consumer;
SingleProducer *producer;
QThread *thread_producer;
;



mainwindow.cpp


#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)

createConsumer();
createProducer();
createConnections();
QTimer::singleShot(1000, producer, SLOT(slot_publishResourceAddress()) );


void MainWindow::slot_log(QString str)

qWarning( QString("LOG> %1").arg(str).toUtf8() );


void MainWindow::createConnections( void )

connect(this, SIGNAL(signal_log(QString)), this, SLOT(slot_log(QString)));
emit signal_log(QString("Log working!"));

connect(producer, SIGNAL(signal_resourceAddress(uint_fast8_t*)), consumer, SLOT(slot_resource(uint_fast8_t*)));


void MainWindow::createProducer( void )

producer = new SingleProducer();
thread_producer = new QThread();
producer->moveToThread(thread_producer); // THIS LINE DESERVES ATTENTION
connect(producer, SIGNAL(signal_log(QString)), this, SLOT(slot_log(QString)));



singleproducer.h


#ifndef SINGLEPRODUCER_H
#define SINGLEPRODUCER_H

#include <QWidget>

class SingleProducer : public QObject

Q_OBJECT
public:
explicit SingleProducer(QObject *parent = nullptr);

signals:
void signal_resourceAddress( uint_fast8_t* addr );
void signal_log(QString str);
public slots:

void slot_publishResourceAddress( void )

emit signal_log( QString("PRODUCER: sent resource address: %1").arg((long int) &un_resources__) );
qWarning(QString("PRODUCER: sent resource address: %1").arg((long int) &un_resources__).toUtf8());
emit signal_resourceAddress( &un_resources__ );



private:
uint_fast8_t un_resources__;
;

#endif // SINGLEPRODUCER_H



The editor doesn't let me post more code... but I think that this is the most relevant part... if not, let me know. But I shared it at pastebin



Where is my mistake?




1 Answer
1



You forgot to actually start the QThreads after creating them in both MainWindow::createProducer and MainWindow::createConsumer. From the documentation of the constructor of QThread:


QThread


MainWindow::createProducer


MainWindow::createConsumer


QThread



Constructs a new QThread to manage a new thread. The parent takes ownership of the QThread. The thread does not begin executing until start() is called.


QThread



So all you need to do is call thread_producer->start() and thread_consumer->start() respectively after creating the threads.


thread_producer->start()


thread_consumer->start()






Thanks @Corristo

– Marcelo Silva
Sep 17 '18 at 17:50



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)