My threadcheck.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;
};
My threadcheck.cpp file is
#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();
}
}
main .cpp is
#include <QCoreApplication>
#include "threadcheck.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ThreadCheck threadCheck;
threadCheck.start();
while(1);
return a.exec();
}
When i start this thread from main , it does not show any output slot never execute. Ideally it should keep printing hello.
Please suggest a solution.