QT 4.8,如何从不同线程发送信号

Qt 4.8, How to send signal from different thread?

本文关键字:线程 信号 QT      更新时间:2023-10-16

我正在尝试通过线程发送信号。

为了测试这种情况,我编写了一个测试代码。我正在使用Ubuntu 16.04机器和QT版本为4.8。

在我的代码中,存在三个类:

1- timer_class->在此类中,我在超时插槽中发出信号。

2 -test_worker->我正在使用此类作为线程的工作。

3 -main_class->我创建timer_class实例,也可以在此类构造函数中创建线程。

我试图将timer_class信号连接到test_worker插槽。

这是我的代码:

First; timer_class :

标题文件:

#ifndef TIMER_CLASS_H
#define TIMER_CLASS_H
#include <QTimer>
#include <QDebug>
#include <QObject>
class timer_class : public QObject
{
    Q_OBJECT
public:
    timer_class(QObject *parent = 0);
    ~timer_class();
    void start_timer();
signals:
    void dummy_signal();
private slots:
    void on_timeout_occur();
private:
    QTimer *timer;
};
#endif // TIMER_CLASS_H

源文件:

#include "timer_class.h"
timer_class::timer_class(QObject *parent)
    :QObject(parent)
{
    timer = new QTimer();
    connect(timer, SIGNAL(timeout()), this, SLOT(on_timeout_occur()));
}
timer_class::~timer_class()
{
}
void timer_class::start_timer()
{
    timer->start(1000);
}
void timer_class::on_timeout_occur()
{
    qDebug() << "timeout occur";
    emit dummy_signal();
}

第二,thread_worker类:标题文件:

#ifndef THREAD_WORKER_H
#define THREAD_WORKER_H
#include <QDebug>
#include <QObject>
class thread_worker : public QObject
{
    Q_OBJECT
public:
    thread_worker(QObject *parent = 0);
    ~thread_worker();
public slots:
    void main_loop();
    void on_dummy_signal();
};
#endif // THREAD_WORKER_H

源文件:

#include "thread_worker.h"
thread_worker::thread_worker(QObject *parent)
    :QObject(parent)
{
}
thread_worker::~thread_worker()
{
}
void thread_worker::main_loop()
{
    forever
    {
        //qDebug() << "In Main Loop";
    }
}
void thread_worker::on_dummy_signal()
{
    qDebug() << "dummy signal received";
}

and last,main_class:标题文件:

#ifndef MAIN_CLASS_H
#define MAIN_CLASS_H
#include <QObject>
#include <QDebug>
#include <QThread>
#include "timer_class.h"
#include "thread_worker.h"
class main_class : public QObject
{
    Q_OBJECT
public:
    main_class(QObject *parent = 0);
    ~main_class();
private:
    QThread *thread;
    timer_class *tmr_class;
    thread_worker *worker;
};
#endif // MAIN_CLASS_H

源文件:

#include "main_class.h"
main_class::main_class(QObject *parent)
    :QObject(parent)
{
    tmr_class   = new timer_class();
    worker      = new thread_worker();
    thread      = new QThread();
    worker->moveToThread(thread);
    connect(tmr_class, SIGNAL(dummy_signal()), worker, SLOT(on_dummy_signal()));
    connect(thread, SIGNAL(started()), worker, SLOT(main_loop()));
    thread->start();
    tmr_class->start_timer();
}
main_class::~main_class()
{
}

在main.cpp中,我只是创建main_class实例:

#include <QCoreApplication>
#include "main_class.h"
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        main_class *main_cl = new main_class();
        qDebug() << "executing a.exec";
        return a.exec();
    }

当我运行应用程序时,我在控制台中看到"超时发生",但我没有看到"收到的虚拟信号"。我找不到问题。

您能帮我这个问题吗?谢谢。

基本问题是您正在创建一个新的QThread,将timer_class实例移至该线程上,然后在线程启动时调用thread_worker::main_loop。由于thread_worker::main_loop基本上是忙碌的循环...

void thread_worker::main_loop ()
{
  forever
  {
    //qDebug() << "In Main Loop";
  }
}

... QThread永远不会有机会处理事件,从而阻止了通过排队连接接收的任何信号。

所有这些的正确修复在很大程度上取决于您希望thread_worker::main_loop做什么(如果有(。

要使事情在此期间进行,只需删除forever循环或评论行...

connect(thread, SIGNAL(started()), worker, SLOT(main_loop()));

完成后,您应该看到"收到的虚拟信号"消息。