Re: [PyQt] Synchronous WebView.load()

2010-07-02 Thread alanm
On Friday 02 July 2010 3:04:40 am you wrote:
> On Fri, 02 Jul 2010 00:31:40 -0500, alan moore  wrote:
> >> As I understand it, the webview doesn't actually load the page and
> 
> update
> 
> >> itself until the method call returns; at which point it loads the new
> >> page and
> >> updates the history.
> 
> No, the load starts straight away but may not finish until some time later
> (probably after the method returns).
> 
> The view will update itself when it has enough of the page loaded to be
> usefully displayed.
> 
> The display won't actually change until the even loop runs.
> 

Thanks for the response.  From my experimenting with this, apparently the 
history doesn't update until after the display does.  I tried something like 
this:

def reset_browser(self):
self.webview.load(self.homepage)
while self.webview.history().currentItem().url() != self.homepage:
pass
self.webview.history().clear()

With the idea being that I'd wait until the history had changed to clear it, 
but it never changed.  The while loop went on infinitely.

> > To illustrate the problem I'm having a little more clearly, I've
> > attached a sample script.  The script is supposed to conduct a brief
> > tour of a list of websites when you hit the "go" button on the browser
> > toolbar.  Each site shows for five seconds, then the next on loads.
> > 
> > Only that's not what happens.  If you run it, you'll see that hitting
> > "go" causes the browser to hang for about 20 seconds, then the final
> > comment is all that shows.  In other words, for the whole duration of
> > the method call, the main window is asleep.
> > 
> > I guess this is just another instance of the trouble I was running into
> > on my other program, which I posted a demonstration script for earlier
> > today.  I would really appreciate any help on this matter.
> 
> You need to get rid of your loop and sleep() and connect to the
> QWebView.loadFinished() signal. In the connected slot start a timer for 5
> seconds. When that timer times out you can then set the next URL.
> 
> Phil

I can see how that works for this example.  In the case of my original 
program, I'm not so sure.  I can't connect webview.history().clear() to 
loadFinished(), or else every single page load will clear the history.  And I 
can't connect and disconnect in the same callback, because it won't be 
processed until the callback exits.

Thanks again for your help.
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Synchronous WebView.load()

2010-07-02 Thread Phil Thompson
On Fri, 02 Jul 2010 00:31:40 -0500, alan moore  wrote:
>> As I understand it, the webview doesn't actually load the page and
update
>> itself until the method call returns; at which point it loads the new
>> page and
>> updates the history.

No, the load starts straight away but may not finish until some time later
(probably after the method returns).

The view will update itself when it has enough of the page loaded to be
usefully displayed.

The display won't actually change until the even loop runs.

> To illustrate the problem I'm having a little more clearly, I've 
> attached a sample script.  The script is supposed to conduct a brief 
> tour of a list of websites when you hit the "go" button on the browser 
> toolbar.  Each site shows for five seconds, then the next on loads.
> 
> Only that's not what happens.  If you run it, you'll see that hitting 
> "go" causes the browser to hang for about 20 seconds, then the final 
> comment is all that shows.  In other words, for the whole duration of 
> the method call, the main window is asleep.
> 
> I guess this is just another instance of the trouble I was running into 
> on my other program, which I posted a demonstration script for earlier 
> today.  I would really appreciate any help on this matter.

You need to get rid of your loop and sleep() and connect to the
QWebView.loadFinished() signal. In the connected slot start a timer for 5
seconds. When that timer times out you can then set the next URL.

Phil
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Synchronous WebView.load()

2010-07-01 Thread alan moore



As I understand it, the webview doesn't actually load the page and update
itself until the method call returns; at which point it loads the new page and
updates the history.

To illustrate the problem I'm having a little more clearly, I've 
attached a sample script.  The script is supposed to conduct a brief 
tour of a list of websites when you hit the "go" button on the browser 
toolbar.  Each site shows for five seconds, then the next on loads.


Only that's not what happens.  If you run it, you'll see that hitting 
"go" causes the browser to hang for about 20 seconds, then the final 
comment is all that shows.  In other words, for the whole duration of 
the method call, the main window is asleep.


I guess this is just another instance of the trouble I was running into 
on my other program, which I posted a demonstration script for earlier 
today.  I would really appreciate any help on this matter.
#!/usr/bin/python
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

from time import sleep
import sys

class MyBrowser(QMainWindow):
"""My Browser demonstrates a problem I'm having with manipulating a webview via called methods
"""

def __init__(self):
"""
Build a browser workshop
"""
super(QMainWindow, self).__init__()
self.webview = QWebView()
self.setCentralWidget(self.webview)
self.navbar = self.addToolBar("Navigation")

#Not totally necessary, but let's make it look like a browser
self.navbar.addAction(self.webview.pageAction(QWebPage.Back))
self.navbar.addAction(self.webview.pageAction(QWebPage.Forward))
self.navbar.addAction(self.webview.pageAction(QWebPage.Reload))
self.navbar.addAction(self.webview.pageAction(QWebPage.Stop))

self.go_button = QPushButton("Go")
self.navbar.addWidget(self.go_button)
self.connect(self.go_button, SIGNAL("clicked()"), self.tour_of_internet)
self.webview.setHtml("Click 'go' to get a tour of the best sites on the Internet.")

def tour_of_internet(self):
"""
This is supposed to give you a tour of several sites, for five seconds each.
"""

sites = ["http://www.google.com";, "http://doc.qt.nokia.com";, "http://www.alandmoore.com";, "http://www.kde.org";]

for site in sites:
self.webview.setUrl(QUrl(site))
self.webview.update()
sleep(5)

self.webview.setHtml("Wow, wasn't that awesome???")

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyBrowser()
w.show()
app.exec_()


___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Re: [PyQt] Synchronous WebView.load()

2010-07-01 Thread alanm
On Thursday 01 July 2010 2:13:47 pm alanm wrote:
> I have an app with a QWebView, and I'm trying to implement a "reset" button
> that will clear the history and return the user to the start page.
> 
> The method looks like this:
> 
> def reset_browser(self):
> self.webview.load(self.startUrl)
> self.webview.history().clear()
> 

After some experimenting, it turns out that the problem isn't quite what I 
thought it was.  In fact, it's almost the opposite problem.

I put self.webview.load() in a while loop so that it would continue to load 
while the history.currentItem().url() is not equal to the startUrl().  The 
loop turned out to be infinite.

As I understand it, the webview doesn't actually load the page and update 
itself until the method call returns; at which point it loads the new page and 
updates the history.

It seems like there's no way to do what I want in one function then, is there?
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


[PyQt] Synchronous WebView.load()

2010-07-01 Thread alanm
I have an app with a QWebView, and I'm trying to implement a "reset" button 
that will clear the history and return the user to the start page.

The method looks like this:

def reset_browser(self):
self.webview.load(self.startUrl)
self.webview.history().clear()

The problem with the method is that QWebView.load() is asynchronous; so the 
page starts loading, then the history clears, then the page finishes loading 
and the previous page is put in to history.  The net result is that the last 
page visited before the reset is still in history.

I can see solutions to this problem if there is a way to:
  - Tell load() to run synchronously
  - Tell history to clear everything, including the current page

Any thoughts?
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt