Qt中绑定信号槽之后,信号槽无效

下面程序编译没有错误,运行却未达到想要的效果,最后调试发现,是信号槽绑定后无效,即槽函数没有受到信号的触发。具体代码如下:
main.cpp

#include <QtCore>
#include "controller.h"
#include "mythread.h"


int main(int argc,char*argv[])
{
    QCoreApplication app(argc,argv);
    Controller controler;
    controler.startThread();

    return app.exec();
}

controller.h

#ifndef CONTROLLER_H
#define CONTROLLER_H

#include <QObject>
#include <QThread>

class mythread;

class Controller : public QObject
{
    Q_OBJECT
public:
    explicit Controller(QObject *parent = nullptr);

    void startThread();

    void stopThread();

signals:
    void threadStopped();

    void threadStarted();

    void threadHasStopped();
public slots:
    void slot_dealThreadStarted();

    void slot_timeout();

//    void slot_threadStopped();

//    void slot_threadHasStopped();
private:
    QThread thread;
    mythread *workThread;
};

#endif // CONTROLLER_H

controller.cpp

#include "controller.h"
#include "mythread.h"
#include <QDebug>
#include <QTimer>

Controller::Controller(QObject *parent) : QObject(parent)
{

}

void Controller::startThread()
{
    workThread = new mythread;
    workThread->moveToThread(&thread);
    connect(&thread,&QThread::finished,workThread,&mythread::deleteLater);
    connect(this,&Controller::threadStarted,this,&Controller::slot_dealThreadStarted);
    connect(this,&Controller::threadStopped,workThread,&mythread::slot_threadStopped,Qt::BlockingQueuedConnection);
    connect(this,&Controller::threadHasStopped,workThread,&mythread::slot_threadHasStopped,Qt::BlockingQueuedConnection);
//    connect(this,&Controller::threadHasStopped,this,&Controller::slot_threadHasStopped);
//    connect(this,&Controller::threadStopped,this,&Controller::slot_threadStopped);
    thread.start();
    emit threadStarted();
    QTimer *timer = new QTimer;
    connect(timer,&QTimer::timeout,this,&slot_timeout);
//    QTimer timer;
//    connect(&timer,&QTimer::timeout,this,&slot_timeout);
    timer->start(5000);
}

void Controller::stopThread()
{
    if(thread.isRunning())
    {
        thread.quit();
        thread.wait();
        emit threadStopped();
//        timer->stop();
        qDebug()<<"线程被关闭!";
        return ;
    }
    emit threadHasStopped();
}

void Controller::slot_dealThreadStarted()
{
    qDebug()<<tr("线程开始执行");
}

void Controller::slot_timeout()
{
    qDebug()<<tr("5s之后:");
    stopThread();
}

//void Controller::slot_threadStopped()
//{
//    qDebug()<<tr("线程退出!");
//}

//void Controller::slot_threadHasStopped()
//{
//    qDebug()<<tr("线程已经执行完毕!");
//}

myThread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QObject>

class mythread : public QObject
{
    Q_OBJECT
public:
    explicit mythread(QObject *parent = nullptr);

public slots:
    void slot_threadStopped();
    void slot_threadHasStopped();

};
#endif // MYTHREAD_H

myThread.cpp

#include "mythread.h"
#include <QDebug>

mythread::mythread(QObject *parent) : QObject(parent)
{

}

void mythread::slot_threadStopped()
{
    qDebug()<<tr("线程退出!");
}

void mythread::slot_threadHasStopped()
{
    qDebug()<<tr("线程已经执行完毕!");
}

其中信号槽绑定文本中,下面这两个信号槽绑定没有实现想要的效果,似乎是信号槽无效。考虑到槽函数与信号不是在同一个线程中,所以在信号槽绑定无效的情况下,设置了第五个参数,但依旧没有达到效果。

connect(this,&Controller::threadStopped,workThread,&mythread::slot_threadStopped,Qt::BlockingQueuedConnection);
connect(this,&Controller::threadHasStopped,workThread,&mythread::slot_threadHasStopped,Qt::BlockingQueuedConnection);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

肩上风骋

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值