Event loop in library
-
Hi
I'm writing shared library (dll under Windows so far) and got problem with event loop. I want to use timer events, so i need this loop enabled. I'm trying to call QCoreApplication::exec() method inside new thread created inside library, but it returns with value -1. My overal structure looks like that:class Worker : public QObject { Q_OBJECT public: Worker() { timer.setInterval(2000); connect(&timer, SIGNAL(timeout()), SLOT(timeout())); timer.start(); } private slots: void timeout() { } private: QTimer timer; }; class XPThread : public QThread { Q_OBJECT void run() Q_DECL_OVERRIDE { Worker work; int res = QCoreApplication::exec(); } };
Any help is highly appreciated.
-
Hi @directx11,
Is your DLL going to be used by a program that already runs Qt?
- If yes, then you cannot use QCoreApplication in your thread. Use a QThread, like @SGaist suggested.
- If no, then you must instantiate a QCoreApplication in your thread first, before you call
QCoreApplication::exec()
. (Also, you cannot use QThread. Use astd::thread
instead)
-
Looks fine right now regarding instantiating QCoreApplication object. Noticed, that events are working, when they are created in the same thread as QCoreApplication, but others, from main thread do not. From the other side cannot move objects from main thread to working one due to some OpenGL issues. So, it won't be possible to use events in main thread even after creation of QCoreApplication object or send events from one thread to another? Everything has to be done in working thread?
-
@directx11 said:
Noticed, that events are working, when they are created in the same thread as QCoreApplication, but others, from main thread do not.
How do you create those events? Please show us your code.