PDA

View Full Version : Event loop in Qt-based shared library



sarabito
11th September 2009, 21:25
Hi,

I'm creating a shared library that is based on a lot of non-GUI Qt components, most notably QHttp. Since the library uses QHttp, I need to have an event loop running. The user of the shared library should be able to instantiate the library object and then continue doing whatever in their code.

I've read that I need a QCoreApplication instance to have an event loop and that QCoreApplication needs to be started from the main thread. However if I call QCoreApplication::exec() from my main thread, control never goes back to the user of the library. This doesn't give me what I want!

I've been struggling with the best way to make this work. The best I can get working is to use a QThread (without an event loop) to start QCoreApplication::exec in QThread::run(). I get a warning about QCoreApplication not being created in the main thread, but it's working.

Any ideas on a better way to implement this? Thanks!

muny
27th September 2009, 16:45
Hello, sarabito.

It is my understanding that "main thread" is not "root process(main() function is running on this thread)".
I create an other thread(this is gui thread, i.e. = main thread) form root process, and app.exe() is running on the thread.
Then, I emit signal on root process to qt based object on main thread.
This method is working now.

Note that I am using only non-gui qt function. Gui qt function do not test.

Thanks and sorry for my poor english.


=======example==========
void taskPoint(void)
{
QCoreApplication();
...
app.exe();
}

int main(void)
{
create pthread (taskPoint);
///
your programs.
///
return EXIT_SUCCESS;
}