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
#endif
class Base
{
public:
Base()
{
#if USE_LOOP2 == 1
#endif
};
virtual void foo() = 0;
{
#if USE_LOOP2 == 1
return loop2;
#else
if (loop) {
return loop;
} else {
}
#endif
}
virtual ~Base(){};
private:
#if USE_LOOP2 != 1
#endif
};
#if USE_LOOP2 != 1
#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[])
{
A a;
QTimer::singleShot(6000, a.
eventLoop(),
SLOT(quit
()));
a.foo();
QTimer::singleShot(6000,
&app,
SLOT(quit
()));
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks