Qt4.5: using localsocket based on event loop before app.exec

I ran into a practical problem with Qt. I use a class that interacts with QLocalSocket with another process (pipe / unix sockets), and I need to make this message before other events occur, that is, before app.exec () starts (or, more precisely, right after the application starts ) The class I'm using requires an eventloop, so it doesn't work if I call the class methods before the event loop starts. Is there some way to trigger something when the event loop is ready? I was thinking about creating a hidden window for events only and fulfilled my duties in the designer of the hidden window and set this window as filling.

Basically, I need this local socket communication task to start as soon as the event loop becomes available.

Any ideas?

Thanks.

+3
source share
2 answers

You can run a separate eventloop using QEventLoop before calling QApplication :: exec (). You must emit a "ready" signal from your class and connect it to the quit () slot of QEventLoop or use the existing signal provided in the Qt class that you are using.

Here is a simple example of getting a web page using QNetworkAccessManager:

app = QtCore.QCoreApplication([])
manager = QtNetwork.QNetworkAccessManager()
req = QtNetwork.QNetworkRequest(QtCore.QUrl("http://www.google.com"))
resp = manager.get(req)
eventloop = QtCore.QEventLoop()
eventloop.connect(resp, QtCore.SIGNAL('finished()'), QtCore.SLOT('quit()'))

eventloop.exec_() # this will block until resp emits finished()

print resp.readAll()

app.exec_()

As long as this can satisfy your needs, I could not understand why you cannot just do everything that you have before calling show () in your window, once that is done, call show ().

+7
source

, 0 :

QTimer::singleShot(0, commsInstancePtr, SLOT(startCommunication()));

, , , Daniel .

+2

Source: https://habr.com/ru/post/1723192/