PDA

View Full Version : QCoreApplication... In a library.



hickscorp
22nd January 2010, 12:33
Hello guys,

i'm creating a library for a windev application. Basically, i need this library to spawn a thread, listen for stuff inside this thread, and make results available in a queue.

So far i've created a bunch of functions exported by the DLL, the first one to make sure a QCoreApplication is created, the second one to destroy it when the windev program is done with the library.
QCoreApplication *coreApp = 0;
DLLIFACE void SCPrepareLibrary () {
if (!coreApp) {
int argc = 0;
coreApp = new QCoreApplication(argc, 0);
}
}
DLLIFACE void SCFreeLibrary () {
if (coreApp) {
coreApp->exit();
delete coreApp;
coreApp = 0;
}
}

Now, i'm wondering... i know i've got to call exec on the coreApp so it handles it's event loop, but since i don't want neither of my DLL functions to be blocking, i don't know what to do.
The solution i have in mind would be to add coreApp->exec() in my preparation function, and ask the developers of the windev application to always call this one in a thread.

Can someone please tell me the direction to take? Any sample code, any webpage talking about how to make the event loop working in a DLL would be very appreciated...

hickscorp
22nd January 2010, 16:34
Bump...

i've done more and more testing, and now i'm kind of lost about what i'm supposed to do:
- i've created a function in my library, which just creates a QCoreApplication and exec() it. This function is called within a thread spawned by the Windev application (Because it needs to be non-blocking).
- This function is called from within the windev application, inside of a thread.
- Then, the windev application calls other functions from my library, which try to create Qt objects giving them the QCoreApplication as parent.
In this case, the qobjects complains they are not in the same thread.

Of course, if my DLL was a standalone application, i would be able to create a QApplication at the beginning, and then all the QObjects would be children of it, because spawned from the same thread... But i have no control over the Windev application, the only things i'm allowed to do is call my DLL functions from it.

What's the logic to achieve such a thing then?
Any kind of help appreciated...
Thanks!

hickscorp
22nd January 2010, 17:26
Ok in other words: is it possible to get a QCoreApplication object from the "already running" application, which already have a message loop? Is there some way for example to do:
QCoreApplication *runningApp = QCoreApplication::fromThread(QThread::currentThrea d()); ?

hickscorp
23rd January 2010, 13:43
i've read a bit about the processEvent method of a QApplication... But i hardly can find how can this help me.
If want to use processEvent, it means it will be called from a function of my DLL, after the Windev application calls it... Which means, i still have no way to poll the device events when the Windev application has control (When the program from my DLL is not running at all, not called, or returned already).
Also, i'd like to be able to use slots and signal functionality, that's kinda why i choose Qt over the so horrible Win32 API.

Still looking for an answer.
Thanks guys,
Me.

umituzun84
23rd January 2010, 14:02
Hi hickscorp;

I have same problem as you have. I have a main MFC application and QT dll which has internal QApplication. When I load qt dll as you expect I have to initialize QApplication and exec it. When I exec qapplication in my dll function, execition flow stops there and never return to my called place to follow other operation in my main MFC application. I can't find any way to solve this wierd problem. You can see my problem from attached image.

is there anyone who knows how to create Qt Dll library with QApplication but when it loaded it can initialize QApplication and exec it instead of expilicitly exported dll function calling from main application. For example MFC dlls can do this operation and when dll loaded it can create own eventloop implicitly so we have not to call another function to initialize any eventloop.

Please help me. I have to solve this problem as fast as I can.

Regards.

Coises
23rd January 2010, 17:29
Can someone please tell me the direction to take? Any sample code, any webpage talking about how to make the event loop working in a DLL would be very appreciated...

I think some of the ideas and information here:
Qt/MFC Migration Framework (http://qt.nokia.com/products/appdev/add-on-products/catalog/4/Windows/qtwinmigrate)
could be useful. Regardless of whether your hosting application uses MFC, the methods used to integrate a Qt message loop with an existing message loop might be relevant.