qt 线程问题 信号触发 使其 槽函数在子线程中运行

系统:win10

qtcreater:5.12.10

条件:使用this->moveToThread( &m_thread );//m_thread 为QThread 的对象

//this 为一个继承Qobject的类 此例中为ADThread

目标 触发信号 使其槽函数在m_thread (子线程)中运行。

现象:1:类中 使用 ADThread 的信号触发的化 只能在其成员方法中触发 可以

           2:类外 使用ADThread 继承基类的信号触发

           3:类外:使用发送类的信号额可以

            4:类外  直接发送ADThread 的信号 其槽函数不能再西线程中运行

结论:若想 使用信号 触发的槽函数再子线程运行 可以使用外部信号 或在类内部 发送信号触发。

在类外使用触发类的信号,其槽函数不能在 子线程中(m_thread)运行

//testobject.h

#ifndef TESTOBJECT_H
#define TESTOBJECT_H
#include <QObject>
class testObject : public QObject
{
    Q_OBJECT
public:
    testObject(){}
    virtual ~testObject(){}
signals:
    testSingl();
};
#endif // TESTOBJECT_H
//adthread.h
#ifndef ADTHREAD_H
#define ADTHREAD_H
#include <QThread>
#include "testobject.h"

class ADThread : public testObject
{   
        Q_OBJECT
public:    
        ADThread();    
        ~ADThread();   
        void startThread2();   
        void startThread3();
signals:   
        void signal_1( );    
        void signal_2( );    
        void signal_3( );
public slots:    
        void slotsignal_1( );   
        void slotsignal_2( );    
        void slotsignal_3( );    
        void slotsignal_4( );
private:    
        QThread m_Analysis;
};
#endif // ADTHREAD_H

//adthread.cpp

#include "adthread.h"

#include <QDebug>

#include <QCoreApplication>

#include <QDateTime>

ADThread::ADThread( )
{    
qDebug()<<"ADThread::ADThread = "<<QThread::currentThreadId();
    this->moveToThread( &m_Analysis );    
    connect(this, &ADThread::signal_2, this, &ADThread::slotsignal_2  );  
    connect(this, &ADThread::signal_3, this, &ADThread::slotsignal_3  );        m_Analysis.start();
}
 
ADThread::~ADThread()
{
    m_Analysis.quit();    
    m_Analysis.wait();
}
void ADThread::slotsignal_1( )// 绑定的信号在外部 ,不能在线程中 执行
{    
qDebug()<<"ADThread::slotsignal_1 = "<<QThread::currentThreadId();
}
 
void ADThread::slotsignal_2( )
{
    qDebug()<<"ADThread::slotsignal_2 = "<<QThread::currentThreadId();    
    QTime delayTime = QTime::currentTime().addSecs( 10 );
    while( QTime::currentTime() < delayTime )//    
    {        
        //QCoreApplication::processEvents(QEventLoop::AllEvents,1000);// 不加 占用Cpu                QThread::msleep(1000);//占用Cpu    
    }
    qDebug() << "slotsignal_1 finsth";
}
 
void ADThread::slotsignal_3( )
{
    qDebug()<<"ADThread::slotsignal_3 = "<<QThread::currentThreadId();    
    qDebug() << "slotsignal_3";
}
 
 void ADThread::slotsignal_4( )
 {     
        qDebug()<<"ADThread::slotsignal_4 = "<<QThread::currentThreadId();     
        qDebug() << "slotsignal_4"; 
}
 
void ADThread::startThread2(){    
        emit signal_2( );
}
void ADThread::startThread3(){
    emit signal_3( );
}
//mianwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "adthread.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)   : QMainWindow(parent)    , ui(new UainWindow){    
        ui->setupUi(this);
    m_pThread = new ADThread;    
connect( m_pThread, &ADThread::signal_1, m_pThread, &ADThread::slotsignal_1, Qt::QueuedConnection );    
connect( m_pThread, &ADThread::testSingl, m_pThread, &ADThread::slotsignal_4 );
}

MainWindow::~MainWindow(){
    //m_pThread->deleteLater();//这样崩溃    
    delete m_pThread;
    delete ui;
}
void MainWindow::on_pushButton_clicked(){
    emit m_pThread->slotsignal_1();    
    qDebug() << "emit slotsignal_1";
}
 
void MainWindow::on_pushButton_2_clicked(){
    //emit m_pThread->slotsignal_2();//这个 槽函数 还是在主线程运行    
    m_pThread->startThread2();//在其成员函数中 触发 信号 后 其槽函数 可以在子线程中运行
}
void MainWindow::on_pushButton_3_clicked(){
    m_pThread->startThread3();
}
void MainWindow::on_pushButton_4_clicked(){    
        emit m_pThread->testSingl();
            qDebug() << "emit slotsignal_4";
}
void MainWindow::on_pushButton_5_clicked(){
    emit signal_5();    
    qDebug() << "emit slotsignal_5";
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

woquNOKIA

谢谢老板!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值