danics
7th March 2017, 10:03
Hi everybody!
I want to know if I emit a signal from another thread which event loop is responsible for emitting the signal. in other words, this signal emitted from which thread?
for example mysignal in below code
class O2;
class O1 : public QObject
{
Q_OBJECT
public:
O1(QObject *parent = 0): olocal(new O2(this)), QObject(parent)
{
m_thread = new QThread;
olocal->moveToThread(m_thread);
connect(m_thread, SIGNAL(started()), olocal, SLOT(start()));
connect(olocal, SIGNAL(finished()), m_thread, SLOT(quit()));
....
}
~O1() {
olocal->finish();
m_thread->wait();
delete olocal;
delete m_thread;
}
signals:
void mysignal();
};
class O2 : public QObject
{
Q_OBJECT
public:
O2(O1 *parent) : q_ptr(parent) {}
signals:
void finished();
public slots:
void start() { b_start = true; QTimer::singleShot(0, this, SLOT(run())); }
void finish() { b_finish = true; }
void run() {
if(b_finished) {
emit finished();
return;
}
// do some staff
if( /* something */)
emit q_ptr->mysignal();
if(b_start)
QTimer::singleShot(0, this, run());
}
};
thanks in advance
I want to know if I emit a signal from another thread which event loop is responsible for emitting the signal. in other words, this signal emitted from which thread?
for example mysignal in below code
class O2;
class O1 : public QObject
{
Q_OBJECT
public:
O1(QObject *parent = 0): olocal(new O2(this)), QObject(parent)
{
m_thread = new QThread;
olocal->moveToThread(m_thread);
connect(m_thread, SIGNAL(started()), olocal, SLOT(start()));
connect(olocal, SIGNAL(finished()), m_thread, SLOT(quit()));
....
}
~O1() {
olocal->finish();
m_thread->wait();
delete olocal;
delete m_thread;
}
signals:
void mysignal();
};
class O2 : public QObject
{
Q_OBJECT
public:
O2(O1 *parent) : q_ptr(parent) {}
signals:
void finished();
public slots:
void start() { b_start = true; QTimer::singleShot(0, this, SLOT(run())); }
void finish() { b_finish = true; }
void run() {
if(b_finished) {
emit finished();
return;
}
// do some staff
if( /* something */)
emit q_ptr->mysignal();
if(b_start)
QTimer::singleShot(0, this, run());
}
};
thanks in advance