PDA

View Full Version : static QEventLoop strange behavior



SABROG
29th July 2009, 11:30
I have this code:



#include <QtCore/QCoreApplication>
#include <QtCore/QtGlobal>
#include <QtCore/QtDebug>
#include <QtCore/QEventLoop>
#include <QtCore/QTimer>

#define USE_LOOP2 0

#if USE_LOOP2 == 1
static QEventLoop *loop2=0;
#endif

class Base
{
public:
Base()
{
#if USE_LOOP2 == 1
loop2 = new QEventLoop;
#endif
};
virtual void foo() = 0;
static QEventLoop * eventLoop()
{
#if USE_LOOP2 == 1
return loop2;
#else
if (loop) {
return loop;
} else {
return new QEventLoop(qApp);
}
#endif
}
virtual ~Base(){};
private:
#if USE_LOOP2 != 1
static QEventLoop *loop;
#endif
};

#if USE_LOOP2 != 1
QEventLoop *Base::loop=0;
#endif

class A : public Base
{
public:
A(){}
virtual ~A(){}
void foo()
{
qDebug() << "Run exec...";
qDebug() << eventLoop()->exec();
qDebug() << "Exit exec...";
}
};

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
A a;
QTimer::singleShot(6000, a.eventLoop(), SLOT(quit()));
a.foo();
QTimer::singleShot(6000, &app, SLOT(quit()));
return app.exec();
}




If i use global static pointer loop2 - all work. If i use static loop pointer as member for class Base - event never exit (in my other program exec() just return -1 value). For test you can change USE_LOOP2 to 0 or 1 and try yourself.

caduel
29th July 2009, 11:57
assign the "new QEventLoop" to your variable loop in or before line 31...

SABROG
29th July 2009, 12:04
Thanks, solved. My mistake, need more sleep.