我的线程检查.h
#include <QThread>
#include <QDebug>
#include <QMutex>
class ThreadCheck : public QThread
{
Q_OBJECT
public:
explicit ThreadCheck(QObject *parent = 0);
int Val() const;
signals:
void signalReceived();
protected:
void run();
public slots:
void slotReceived();
private:
QMutex mutex;
int num;
};
我的 threadcheck.cpp 文件是
#include "threadcheck.h"
ThreadCheck::ThreadCheck(QObject *parent) :
QThread(parent)
{
connect(this,SIGNAL(signalReceived()),this,SLOT(slotReceived()));
num = 0;
}
int ThreadCheck::Val() const
{
return num;
}
void ThreadCheck::slotReceived()
{
mutex.lock();
qDebug() << "hello";
mutex.unlock();
}
void ThreadCheck::run()
{
while(1)
{
emit signalReceived();
}
}
主要的.cpp是
#include <QCoreApplication>
#include "threadcheck.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ThreadCheck threadCheck;
threadCheck.start();
while(1);
return a.exec();
}
当我从 main 启动这个线程时,它没有显示任何输出槽永远不会执行。理想情况下,它应该继续打印你好。
请提出解决方案。