QT:暂停功能流程,直到 QWebView SIGNAL 完成
我主要是一名网络开发人员,所以我的问题可能是某种初学者。
我正在编写一个与 QWebView 内容一起使用的函数,这是一种在加载的网页上执行操作的宏脚本。
代码是这样的:
somefunction() {
QWebView *webView;
webView->load(QUrl("http://www.google.com"));
<...> here I want to pause the code until the page is loaded <...>
here goes the code of html manipulation, click on another link
<...> here I want to pause the code until the page is loaded <...>
}
好的,所以在我写“<...>”的地方我想暂停代码,直到页面加载<...>”我不知道如何暂停该功能并使其等待页面完成加载。我了解信号和槽。但是如果我在这里使用槽,我将不得不在另一个函数中继续 - 在 SLOT 函数中,但我想将所有代码和逻辑留在 somefunction() 中。怎么做呢?
I'm a mostly webdeveloper, so my question can be somekind of a beginners.
I'm writing a function which works with QWebView content, some kind of a macro script which makes action on loaded web page.
code is something like that:
somefunction() {
QWebView *webView;
webView->load(QUrl("http://www.google.com"));
<...> here I want to pause the code until the page is loaded <...>
here goes the code of html manipulation, click on another link
<...> here I want to pause the code until the page is loaded <...>
}
Ok, so in places where I wrote "<...> here I want to pause the code until the page is loaded <...>" I dunno what to do to pause the function and make it wait for page to finish loading. I know about signals and slots. But if I use slot here, I will have to continue in another function - in SLOT function, but I want to leave all the code and logic in somefunction(). How to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用嵌套的 QEventLoop 来同步异步处理,例如页面加载。当发出
loadFinished
时,只需exit()
循环即可从进入循环的位置恢复执行。另一方面,将处理拆分为多个函数(槽)也不是那么糟糕。代码和逻辑仍然位于同一个类中的同一位置。
You can use a nested
QEventLoop
to synchronize asynchronous processing like page loading. WhenloadFinished
is emitted, justexit()
the loop to resume execution from where you entered the loop.On the other hand, splitting processing to multiple functions (slots) isn't that bad either. The code and logic is still in the same place in the same class.