PDA

View Full Version : QLocalServer without QCoreApplication ?



Raistlin
9th May 2008, 22:25
I noticed that in Qt 4.4, you need to construct a QCoreApplication object before a QLocalServer can be used. QLocalServer::listen() does return true even if the QCoreApplication does not exist, but no connections can be made.

I have a Qt DLL which is called from non-Qt code, and there I need to use QLocalServer/QLocalSocket to communicate with a Qt program. When I create a dummy QCoreApplication in this DLL, the communication works, otherwise not. This does however seems to be a dangerous practice. I do not need QCoreApplication::exec() or any other related methods.

The same problem does not occur with TCP sockets. Anyway has any idea how to handle this nicely?

wysota
9th May 2008, 22:44
I noticed that in Qt 4.4, you need to construct a QCoreApplication object before a QLocalServer can be used. QLocalServer::listen() does return true even if the QCoreApplication does not exist, but no connections can be made.
That's perfectly normal. All connection oriented classes need events to be delivered to work.


I do not need QCoreApplication::exec() or any other related methods.
Hmm... really? Oh.. you probably use waitForNewConnection(), right? It spawns an internal event loop...


The same problem does not occur with TCP sockets. Anyway has any idea how to handle this nicely?

Some features need QCoreApplication to work, because there is some initialization code there (for instance plugins are loaded from within the application's constructor, so you need to create it regardless if you're going to use it or not). You might safely create your own application instance, just check if some other code hasn't done it earlier (checking QCoreApplication::instance() for null might be enough).

Raistlin
9th May 2008, 23:03
That does make sense, and indeed, there seems no real harm in it when checking QCoreApplication::instance() first. I do see your point about waitForNewConnection() needing an internal event loop, it is just confusing that for QTcpServer the same method does not need it. But then again, the IPC classes are probably just similar to the TCP classes in interface only.

wysota
9th May 2008, 23:10
it is just confusing that for QTcpServer the same method does not need it.

It does need an event loop running to work.

Raistlin
9th May 2008, 23:15
I meant an instance of CoreApplication, but I guess the event loop is done differently in the network classes and in the local socket classes.

wysota
9th May 2008, 23:17
It has nothing to do with the event loop. The event loop is just to make timers work and the mechanism is the same in both cases. The initialization part might be different - for instance the local socket might need access to the thread id or some other application specific stuff. You might check the sources, you know :)

Raistlin
9th May 2008, 23:44
I know, I know :). Sometimes I forget I have access to the sources, or I just assume I won't figure it out anyway. But maybe I need to check them more often.