PDA

View Full Version : Pause function flow till QWebView SIGNAL is completed



Timus83
21st October 2010, 11:30
Hello!

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->setUrl(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 do I do this?

lyuts
21st October 2010, 12:04
1. Looks like setUrl() doesn't load any page for you. There is load() for that.
2. As for keeping all logic in somefunction() then I think this is not a very good idea. I could find an easy way to stop/resume the execution of you function and then resume it when the page is loaded. I guess, you will have to use singals.

Timus83
21st October 2010, 14:44
Hi!
actually setUrl works, but this is not an issue. How to pause and resume that function? I want it to look like that:


somefunction() {
1. url loaded here
2. check if pageisloaded
3. if page is loaded, let's fill the field and press the button
4. new page is being loaded after the button press
5. we make sure that the page is loaded
6. fill in another field and submit the button
}

I just can't find the way doing this inside one single function, please help. Or, I'd be glad to read some materials or examples on how to create such a program, which fills desired fields and submits forms.

lyuts
21st October 2010, 16:35
If you need a utility to automate form fillings then you may want to look at curl. Some instructions are here (http://www.knowledgesutra.com/forums/topic/38162-automatic-login-using-curl/) and here (www.linuxjournal.com/.../use-curl-login-websites-script).

If you want to write your own application then (as for me) I see here a problem, that loading of a page is not a blocking operation. In this case you will have to sleep and check for the loading. So, here an approximate pseudo code:



somefunction() {
webView->load(yourUrl);

while (!pageLoaded()) {
sleep(nSeconds);
}

// do your processing here
}


So we already have one more function, which is:



bool YourModule::pageLoaded()
{
return mPageLoaded;
}


Obviously mPageLoaded is set to FALSE at the very beginning, so when the page is loaded it should be set to TRUE, so that we exit that while loop. They only way we can do it (at least the only that I found) with the help of QWebView, is connect loadFinished(bool ok) signal of QWebView to your slot and your slot will set mPageLoaded to TRUE, i.e.



connect(youWebView, SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded(bool)));
...
void YourModule::pageLoaded(bool iOk)
{
mPageLoaded = true;
}


This is the solution I see at this point.

Timus83
23rd October 2010, 17:27
Thanks for your hints lyuts, finally I came to solution with QEvenLoop and everything works like a charm!

lyuts
24th October 2010, 06:54
Thanks for your hints lyuts, finally I came to solution with QEvenLoop and everything works like a charm!

So you are using its exit() and exec() functions, right?