Qt4.5:在app.exec之前使用基于事件循环的localsocket [英] Qt4.5: Using event-loop based localsocket before app.exec

查看:192
本文介绍了Qt4.5:在app.exec之前使用基于事件循环的localsocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在面对Qt的实际问题。我正在使用一个与QLocalSocket通信到另一个进程(管道/ unix套接字)的类,我需要在其他事件发生之前进行通信,就是在app.exec()启动之前(或者更准确地说,一旦应用程序启动)。我正在使用的类需要一个eventloop,所以如果在事件循环开始之前调用类方法,它不起作用。当事件循环准备就绪时,有什么办法可以开始吗?我想到了一个隐藏的事件窗口,并在隐藏的窗口构造函数中执行我的任务,并将这个窗口建立为高阶。



基本上,我需要这个本地socket



任何想法?



谢谢。 / p>

解决方案

您可以启动一个单独的事件循环,使用 QEventLoop ,然后调用QApplication :: exec()。您应该从您的课堂中发出一个完成信号,并将其连接到QEventLoop quit()插槽,或使用您正在使用的Qt类中提供的现有信号。



以下是使用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_ )#这将阻塞,直到resp发出完成()

打印resp.readAll()

app.exec_()
/ pre>

虽然这可能适合您的需要,但我不明白为什么您不能在窗口上调用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆