参考链接: https://www.cnblogs.com/flowingwind/p/8361939.html
说明:
1、一下小结不保证对,如果错误希望指正
2、Queue和Direc代表是的connect的链接方式, Qt::DirectConnection和Qt::QueuedConnection
小结:
-
一个线程如果有多个任务要执行,且某个任务会阻塞,导致其他要执行的没有执行,这种情况就需要用线程了. 比如:QTimer 在主线程,主线程等待用户输入会导致定时器信号没法发出去(可通过接收对象在B线程,timer和等待用户输入程序在A线程, 不输入时,B没有执行槽函数证实).
-
A线程中的对象th,如果在线程B中执行th的某函数, 那么是无效的,只有在线程A中执行该对象的函数才可以。可以通过信号与槽实现间接的执行另一个线程对象的某个函数. [这里指的是QTimer对象, 比如, main中直接不用信号与槽直接start和stop时, 都会提示QObject::killTimer: Timers cannot be stopped/started from another thread, 虽然提示了,但是实际上是启动不可以, 停止是可以的]
-
connect可以作用于多个线程, 发送对象发送信号时所执行的线程都是发送对象所在的线程. 接收对象所在的线程取决于类型,如果是Direct,那么槽函数在发送对象所在线程执行,如果是Queue,那么槽函数在接收对象所在线程执行
我想实现的一个小定时器程序: 输入s(start), 定时器启动, 打印信息。 输入e(end), 定时器停止运行.
大概实现思路: 主线程等待用户输入,因为主线程会阻塞timer发送timeout信号,因此需要把timer放到另一个线程中。 接收对象如果也在主线程中定义,那么连接方式可以定义为Direc来保证槽函数在timer的线程中执行。 也可以把接收对象放在timer的线程中, 使用Queue.
代码
#ifndef SIGNALSLOTTEST_H
#define SIGNALSLOTTEST_H
#include <QObject>
#include<QDebug>
#include<QThread>
class SignalSlotTest : public QObject
{
Q_OBJECT
public:
explicit SignalSlotTest(QObject *parent = nullptr);
void stop();
void start();
signals:
void stopTimer();
void startTimer();
public slots:
void onTimeout();
};
#endif // SIGNALSLOTTEST_H
#include "signalslottest.h"
SignalSlotTest::SignalSlotTest(QObject *parent) : QObject(parent)
{}
void SignalSlotTest::stop()
{
qDebug()<<"threadId: "<<QThread::currentThreadId()<<" stop timer"<<endl;
emit stopTimer();
}
void SignalSlotTest::start()
{
qDebug()<<"threadId: "<<QThread::currentThreadId()<<" start timer"<<endl;
emit startTimer();
}
void SignalSlotTest::onTimeout()
{
qDebug()<<"subthreadID: "<<QThread::currentThreadId()<<" timer running"<<endl;
}
main.cpp
采用: Direc时, sst不需要moveToThread; 采用Queue时,sst需要moveTothread
#include<QtCore>
#include<QPoint>
#include<QMap>
#include<QString>
#include<QTimer>
#include<QString>
#include"signalslottest.h"
#include<QObject>
#include<QTextStream>
#include <QApplication>
#include<QThread>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QTextStream cin(stdin);
QString input;
QTimer timer;
SignalSlotTest sst;
QThread timerTh;
// QObject::connect(&timer, SIGNAL(timeout()), &sst, SLOT(onTimeout()), Qt::DirectConnection);
QObject::connect(&timer, SIGNAL(timeout()), &sst, SLOT(onTimeout()));
QObject::connect(&sst, SIGNAL(stopTimer()), &timer, SLOT(stop()));
QObject::connect(&sst, SIGNAL(startTimer()), &timer, SLOT(start()));
timer.moveToThread(&timerTh);
sst.moveToThread(&timerTh);
timer.setInterval(1000);
timerTh.start();
qDebug()<<"mainThreadID: "<<QThread::currentThreadId();
// QThread::msleep(5000);
while(true){
cin>>input;
if(input=="s"){
sst.start();
//timer.start();
}else if(input=="e"){
sst.stop();
//timer.stop();
}
}
return a.exec();
}