C++ 将QObject移动到QThread后,信号不再工作
基本上,标题。。。如果没有QThread(或者只是注释),我会得到以下结果:C++ 将QObject移动到QThread后,信号不再工作,c++,qt,qthread,C++,Qt,Qthread,基本上,标题。。。如果没有QThread(或者只是注释),我会得到以下结果: LOG> Log working! LOG> PRODUCER: sent resource address: 29980624 PRODUCER: sent resource address: 29980624 CONSUMER: received resource address: 29980624 29980624或任何相关的存储器位置 但是,当联合国刚刚发表评论时 LOG> Log work
LOG> Log working!
LOG> PRODUCER: sent resource address: 29980624
PRODUCER: sent resource address: 29980624
CONSUMER: received resource address: 29980624
29980624或任何相关的存储器位置
但是,当联合国刚刚发表评论时
LOG> Log working!
主窗口
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)));
}
单生产者
#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
\ifndef SINGLEPRODUCER\u H
#定义单生产者
#包括
类别:公共QoObject
{
Q_对象
公众:
显式单生产者(QObject*parent=nullptr);
信号:
无效信号资源地址(uint\u fast8\u t*addr);
无效信号测井(QString str);
公众时段:
void槽_publishResourceAddress(void)
{
发出信号日志(QString(“生产者:发送的资源地址:%1”).arg((长int)和un_资源日志));
qWarning(QString(“生产者:发送的资源地址:%1”).arg((长整型)和un_resources_uuuuuuu).toUtf8();
发出信号\u资源地址(&u资源\u);
}
私人:
提供快速8个资源;
};
#endif//SINGLEPRODUCER\u H
编辑器不允许我发布更多代码。。。但我认为这是最相关的部分。。。如果没有,请告诉我。但我在一次聚会上分享了它
我的错误在哪里?在
MainWindow::createProducer
和MainWindow::createConsumer
中创建了QThread
之后,您忘记了实际启动它们。从:
构造一个新的QThread
来管理一个新线程。父线程拥有QThread的所有权在调用start()之前,线程不会开始执行
因此,在创建线程后,您只需分别调用
thread\u producer->start()。从:
构造一个新的QThread
来管理一个新线程。父线程拥有QThread的所有权在调用start()之前,线程不会开始执行
因此,创建线程后,您只需分别调用thread\u producer->start()
和thread\u consumer->start()