E
E
esemi2012-05-02 11:25:10
Python
esemi, 2012-05-02 11:25:10

PyQt4. How to get result of all requests from QWebView?

Greetings habra-community.
I ran into a problem with getting the results of side queries when using QWebView from PyQt4.

I inherit and customize my QWebView:

class MainWindow(QWebView):<br>
    def __init__(self, parent = None):<br>
        super(MainWindow,self).__init__(parent)<br>
        settings = self.settings()<br>
        settings.setAttribute(QWebSettings.JavascriptEnabled,True)<br>
        settings.setAttribute(QWebSettings.PluginsEnabled,True)<br>

I do the same with QNetworkAccessManager:
class CustomManager(QNetworkAccessManager):<br>
    def __init__(self, parent = None):<br>
        super(CustomManager,self).__init__(parent)<br>
        self.connect(self, SIGNAL("finished(QNetworkReply*)"), self.print_response)<br>
   <br>
    def print_response(self, reply):<br>
        print reply.url(), reply.isFinished()<br>
        print reply.readAll() #EMPTY!<br>

And finally, let's start the whole thing:
app = QApplication(sys.argv)<br>
app.setApplicationName("test")<br>
<br>
window = MainWindow()<br>
manager = CustomManager()<br>
window.page().setNetworkAccessManager(manager)<br>
<br>
window.load(QUrl("http://ya.ru/"))<br>
window.show()<br>
sys.exit(app.exec_())<br>

And here it turns out that the line is print reply.readAll()always empty. At the same time, reply.isFinished(), as expected, is True.

At first I decided that I was using the wrong signal and redefined QNetworkAccessManager.createRequest in order to get to the original reply and hook on its finished signal . However, here the result was the same - for all responses, the request was completed, but the result was not read (empty).

I also tried to hang up the result waiting loop in the print_response method, but this led to an endless recursion.

In general, please help and do not kick =)

UPD: the solution is to proxy QNetworkReply. I recommend taking this code as a basis (or its analogue on the pluses )
The only problem with this approach is that changing the content of QWebView caused by various actions (user or javascript) stops working. That is, the first load () is displayed normally, but then the process goes on, but is not shown.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gagoman, 2012-05-02
@gagoman

I collected your example from PySide - everything works.
Python 2.6.6, PySide 1.1.0qt474, Win 7 x86
Tried a slightly different option, namely
instead of:

self.connect(self, SIGNAL("finished(QNetworkReply*)"), self.print_response)

used:
self.finished.connect(self.print_response)

It worked in this case too.
As I understand it, you are waiting in the output for something like: If necessary, I will check for PyQt PS and please do not put semicolons at the end of the line
PySide.QtCore.QUrl('http://www.yandex.ru/data/mail.js?yaru=y') True
PySide.QtCore.QUrl('http://kiks.yandex.ru/system/fc06.html') True
PySide.QtCore.QUrl('http://kiks.yandex.ru/system/fc06.swf') True
PySide.QtCore.QUrl('http://suggest.yandex.ru/jquery-1-4-2.crossframeajax.html') True

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question