Results 1 to 6 of 6

Thread: Pause function flow till QWebView SIGNAL is completed

  1. #1
    Join Date
    Oct 2010
    Posts
    4
    Qt products
    Qt4

    Default Pause function flow till QWebView SIGNAL is completed

    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:

    Qt Code:
    1. somefunction() {
    2.  
    3. QWebView *webView;
    4. webView->setUrl(QUrl("http://www.google.com"));
    5. <...> here I want to pause the code until the page is loaded <...>
    6. here goes the code of html manipulation, click on another link
    7. <...> here I want to pause the code until the page is loaded <...>
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    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?

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Pause function flow till QWebView SIGNAL is completed

    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.
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Oct 2010
    Posts
    4
    Qt products
    Qt4

    Default Re: Pause function flow till QWebView SIGNAL is completed

    Hi!
    actually setUrl works, but this is not an issue. How to pause and resume that function? I want it to look like that:

    Qt Code:
    1. somefunction() {
    2. 1. url loaded here
    3. 2. check if pageisloaded
    4. 3. if page is loaded, let's fill the field and press the button
    5. 4. new page is being loaded after the button press
    6. 5. we make sure that the page is loaded
    7. 6. fill in another field and submit the button
    8. }
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Pause function flow till QWebView SIGNAL is completed

    If you need a utility to automate form fillings then you may want to look at curl. Some instructions are here and here.

    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:

    Qt Code:
    1. somefunction() {
    2. webView->load(yourUrl);
    3.  
    4. while (!pageLoaded()) {
    5. sleep(nSeconds);
    6. }
    7.  
    8. // do your processing here
    9. }
    To copy to clipboard, switch view to plain text mode 

    So we already have one more function, which is:

    Qt Code:
    1. bool YourModule::pageLoaded()
    2. {
    3. return mPageLoaded;
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. connect(youWebView, SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded(bool)));
    2. ...
    3. void YourModule::pageLoaded(bool iOk)
    4. {
    5. mPageLoaded = true;
    6. }
    To copy to clipboard, switch view to plain text mode 

    This is the solution I see at this point.
    I'm a rebel in the S.D.G.

  5. #5
    Join Date
    Oct 2010
    Posts
    4
    Qt products
    Qt4

    Default Re: Pause function flow till QWebView SIGNAL is completed

    Thanks for your hints lyuts, finally I came to solution with QEvenLoop and everything works like a charm!

  6. #6
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Pause function flow till QWebView SIGNAL is completed

    Quote Originally Posted by Timus83 View Post
    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?
    I'm a rebel in the S.D.G.

Similar Threads

  1. how to emit signal in a static function ?
    By cxl2253 in forum Qt Programming
    Replies: 32
    Last Post: 7th July 2016, 22:36
  2. Emit signal from const function
    By waynew in forum Qt Programming
    Replies: 9
    Last Post: 22nd August 2010, 14:10
  3. How to know when the server has completed the painting of the widget...??
    By kapoorsudhish in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 16th April 2010, 14:15
  4. static function emitting signal
    By wagmare in forum Qt Programming
    Replies: 8
    Last Post: 26th March 2010, 09:34
  5. Replies: 8
    Last Post: 27th October 2009, 10:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.