QT 4.8,如何从不同线程发送信号
Qt 4.8, How to send signal from different thread?
我正在尝试通过线程发送信号。
为了测试这种情况,我编写了一个测试代码。我正在使用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()));
完成后,您应该看到"收到的虚拟信号"消息。
相关文章:
- 线程之间的布尔停止信号
- 通过安装信号处理程序关闭多线程应用程序
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 发出信号以从线程更新 UI
- Qt信号和插槽如果从QRunnable或其他线程调用,则不起作用
- 从另一个线程发出信号是否安全?
- boost信号和插槽在不同的线程中不工作(使用boost::asio::io_service)
- Qt:向死/停止线程发送信号
- Qt5 跨线程信号和插槽
- Qt5 多线程:信号仅单向工作
- 将工作线程信号与主窗口插槽 (Qt5) 连接时出错
- 通知有关变量变化的线程(信号?)
- 交叉线程信号插槽,如何发送字符*
- 事务中的线程信号
- 休眠 X 毫秒或线程信号
- 多线程信号
- 事件队列是否与用于跨线程信号/槽(在Qt中)的队列相同?
- Xcode 线程信号错误和本地声明错误
- c++11中的父线程信号
- QT小部件插槽调用没有被单独的线程信号激活