c++ - QObjects 移入 QThreads 后信号不再工作

标签 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 working!

主窗口.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;
};

主窗口.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)));
}

单一生产者.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

编辑器不允许我发布更多代码...但我认为这是最相关的部分...如果没有,请告诉我。但我在 pastebin 分享了它

我的错误在哪里?

最佳答案

MainWindow::createProducerMainWindow::createConsumer 中创建QThread 后,您忘记了实际启动它们。来自documentation of the constructor of 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.

所以你需要做的就是在创建线程后分别调用thread_producer->start()thread_consumer->start()

关于c++ - QObjects 移入 QThreads 后信号不再工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52351035/

相关文章:

c++ - 找不到 Eclipse 和 CDT 二进制文件

c++ - gcc 使用 std::initializer_list 调用构造函数而不是复制省略,而 clang 则不会。哪个编译器是正确的?

c++ - 成员函数模板参数默认值

C# 上下文菜单处理程序

qt - 图标下方带有文本的按钮

c++ - 在 C++ 中获取应用程序窗口的大小

c++ - 对象删除 : use parent or not

python - 在 QTimer Singleshot 之后终止 QThread

python - 如何从 GUI 停止 QThread

c++ - QThread在c++中的基本使用