Can't receive signal from another thread as following.
Using connect type as Qt::BlockingQueuedConnection, the thread blocks. But signal not received.
main.cpp
#include "myclass.h"
int main(int argc, char *argv[])
{
MyClass myClass;
myClass.startAll();
return a.exec();
}
#include "myclass.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyClass myClass;
myClass.startAll();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
thread.h
#include <QtCore/QCoreApplication>
#include <QThread>
#include <QDebug>
{
Q_OBJECT
public:
void run( void )
{
qDebug() << "thread 1 started";
int i = 0;
while(1)
{
msleep( 10 );
i++;
qDebug
() <<
"i is: " + QString::number(i
);
if(i==1000)
{
qDebug() << "Got I";
emit MyThread1Signal();
exit();
}
}
}
signals:
void MyThread1Signal();
};
#include <QtCore/QCoreApplication>
#include <QThread>
#include <QDebug>
class Thread1 : public QThread
{
Q_OBJECT
public:
void run( void )
{
qDebug() << "thread 1 started";
int i = 0;
while(1)
{
msleep( 10 );
i++;
qDebug() << "i is: " + QString::number(i);
if(i==1000)
{
qDebug() << "Got I";
emit MyThread1Signal();
exit();
}
}
}
signals:
void MyThread1Signal();
};
To copy to clipboard, switch view to plain text mode
myclass.cpp
MyClass
::MyClass(QObject *parent
) :{
thread1 = new Thread1();
connect(thread1, SIGNAL(MyThread1Signal()), this, SLOT(myClassSlot1()), Qt::BlockingQueuedConnection);
}
void MyClass::startAll()
{
thread1->start();
thread1->wait();
}
void MyClass::myClassSlot1()
{
qDebug()<<"Signal received";
}
MyClass::MyClass(QObject *parent) :
QObject(parent)
{
thread1 = new Thread1();
connect(thread1, SIGNAL(MyThread1Signal()), this, SLOT(myClassSlot1()), Qt::BlockingQueuedConnection);
}
void MyClass::startAll()
{
thread1->start();
thread1->wait();
}
void MyClass::myClassSlot1()
{
qDebug()<<"Signal received";
}
To copy to clipboard, switch view to plain text mode
myclass.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
#include "threads.h"
{
Q_OBJECT
public:
explicit MyClass
(QObject *parent
= 0);
void startAll();
signals:
public slots:
void myClassSlot1();
private:
Thread1* thread1;
};
#endif // MYCLASS_H
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
#include "threads.h"
class MyClass : public QObject
{
Q_OBJECT
public:
explicit MyClass(QObject *parent = 0);
void startAll();
signals:
public slots:
void myClassSlot1();
private:
Thread1* thread1;
};
#endif // MYCLASS_H
To copy to clipboard, switch view to plain text mode
Bookmarks