PDA

View Full Version : QThread in a library



nimsnx
30th June 2009, 00:17
Hi,

I'm new to Qt and I started writing a library that would perform a simple http operation. When called, the single "public" method for the library will create a QThread, perform an http operation on that thread, and then return to the caller.

It seems that the running of the QThread depends on a QApplication (QEventLoop: Cannot be used without QApplication). Because a QApplication is for managing "the GUI application's control flow and main settings" it doesn't seem that the QApplication should come from the library.

Is it possible to write a library using QHttp and QThread that will link and work successfully both with applications that are and that aren't Qt applications?

If the library can be written so that it will link and work with a non-Qt application, then should the library provide the QApplication for non-Qt apps? Does that even make sense?

Thanks

wysota
30th June 2009, 00:33
Why does the call need to be threaded? Maybe you can use QEventLoop instead? You'll still need an application object, of course.

As for the object itself - you may try to detect if there already is an existing application object or you may ask the user to provide one before you make the main call.

nimsnx
30th June 2009, 16:27
Why does the call need to be threaded?
Pretend for the sake of argument that it does. While I'm asking now about a simple task, I'm trying to understand what I can and can't do with Qt libraries particulary with regard to QThreads.


Maybe you can use QEventLoop instead? You'll still need an application object, of course.

Why is a QApplication object required if I'm not writing an application?


As for the object itself - you may try to detect if there already is an existing application object or you may ask the user to provide one before you make the main call.

But if the user of the library isn't a Qt application and the the library doesn't expose anything related to Qt (all that the caller should know is that if I call this function in this library that it will get an appropriate answer), then how could it provide the application object?


Is it possible to have a QThreaded Qt library that doesn't rely on a QApplication?

Thanks

wysota
30th June 2009, 17:09
Pretend for the sake of argument that it does.
The argument can be meaningless if you start with wrong assumptions.


Why is a QApplication object required if I'm not writing an application?
Because it serves as an initialization point for many important things and also it contains the main event loop.


But if the user of the library isn't a Qt application and the the library doesn't expose anything related to Qt
... then you create your own application object.


Is it possible to have a QThreaded Qt library that doesn't rely on a QApplication?

Threads don't need the application object but other things might (like per thread event loops).