
Originally Posted by
N3wb
Why is there difference between having the loop in the "run()" or "exec()" functions, though? It's the same code in both functions, but for some reason only works in the "exec()" function?
Thanks!
There is nothing difference. Here is code of original QThread::exec method :
{
d->data->quitNow = false;
locker.unlock();
int returnCode = eventLoop.exec();
return returnCode;
}
int QThread::exec()
{
Q_D(QThread);
QMutexLocker locker(&d->mutex);
d->data->quitNow = false;
QEventLoop eventLoop;
locker.unlock();
int returnCode = eventLoop.exec();
return returnCode;
}
To copy to clipboard, switch view to plain text mode
As You see You never create and exec event loop. Yours Thread class must be defined something like that :
{
Q_OBJECT
public:
Thread();
signals:
slots:
void mTimeOut();
protected:
void run();
};
Thread::Thread()
{
}
void Thread::run()
{
QTimer::singleShot(0,
this,
SLOT(mTimeOut
()));
exec();
}
void Thread::mTimeOut()
{
//do what You need in one step
emit(testSignal("hello world!"));
//do sleep and after start next step
QTimer::singleShot(1,
this,
SLOT(mTimeOut
()));
}
class Thread : public QThread
{
Q_OBJECT
public:
Thread();
signals:
void testSignal(QString message);
slots:
void mTimeOut();
protected:
void run();
};
Thread::Thread()
{
}
void Thread::run()
{
QTimer::singleShot(0, this, SLOT(mTimeOut()));
exec();
}
void Thread::mTimeOut()
{
//do what You need in one step
emit(testSignal("hello world!"));
//do sleep and after start next step
QTimer::singleShot(1, this, SLOT(mTimeOut()));
}
To copy to clipboard, switch view to plain text mode
Bookmarks