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?
You can use a nested QEventLoop
to synchronize asynchronous processing like page loading. When loadFinished
is emitted, just exit()
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.
精彩评论