Qt4.5:在app.exec之前使用基于事件循环的localsocket [英] Qt4.5: Using event-loop based localsocket before app.exec
问题描述
基本上,我需要这个本地socket
任何想法?
谢谢。 / p>
您可以启动一个单独的事件循环,使用 QEventLoop ,然后调用QApplication :: exec()。您应该从您的课堂中发出一个完成信号,并将其连接到QEventLoop quit()插槽,或使用您正在使用的Qt类中提供的现有信号。
以下是使用QNetworkAccessManager获取网页的简单示例:
app = QtCore.QCoreApplication([])
/ pre>
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_ )#这将阻塞,直到resp发出完成()
打印resp.readAll()
app.exec_()
虽然这可能适合您的需要,但我不明白为什么您不能在窗口上调用show()之前做任何事情一旦完成,请调用show()。
I'm facing a practical problem with Qt. I'm using a class that communicates with QLocalSocket to another process (pipes/unix sockets) and I need to do that communication before other events occur, that is before app.exec() starts (or more precisely,as soon as app starts). The class that I'm using needs an eventloop so it does not work if I call the class methods before an event loop is started. There is any way to start something when the event loop is ready? I thought of making a hidden event-only window and do my duties in the hidden window constructor, and stablish this window as toplevel.
Basically, I need this local-socket communication task to start as soon as the event loop becomes available.
Any ideas?
Thank you.
解决方案You could start a separate eventloop, using QEventLoop, before calling QApplication::exec(). You should emit a "done" signal from your class and connect that to the QEventLoop quit() slot, or use an existing signal provided in the Qt class you're using.
Here's a simple example fetching a webpage 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_()
While this might suit your needs, I couldn't quite understand why you can't simply do whatever business you have prior to calling show() on your window, once that's done, call show().
这篇关于Qt4.5:在app.exec之前使用基于事件循环的localsocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!