下面程序编译没有错误,运行却未达到想要的效果,最后调试发现,是信号槽绑定后无效,即槽函数没有受到信号的触发。具体代码如下:
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);