QWebView unable to load a page when the content-type isn't set

  • Hi there,

    I'm using a custom web server with a PyQt4 application that embeds a QWebView as a web browser.

    I've noticed that certain pages won't load and, by trial and error, found that if I force my web server to always set the content-type to 'text/html' I can get these pages to load.

    If I try to load these pages in another web browser (Chrome, Safari or Firefox) they work fine without any alterations to the server.

    So, my questions are:-

    1. Is there a way to set a default content-type for a QWebView?
    2. Is it possible to put the QtWebKit module into a debug/logging mode to see if it outputs anything when one of these pages fail to load?
    3. Is there another possible solution for this problem other that changing the server?

    Thanks in advance,

  • Otherwise what happens? Did you investigate which content-types make QtWebkit happy? Is the QWebPage::unsupportedContent fired?

  • Those are limitation of the network stack. It does not do content type detection if the server does not report a valid type.

    If you are on Linux, you can try using KIO, I think it does content type detection.

    If you are just using the view with you server, you can simply subclass QNetworkAccessManager and force the type yourself.

  • Thanks for the replies guys.

    Neither unsupportedContent or loadStarted are firing which is confusing. Does this mean it supports the content, but refuses to load it..?

    @Benjamin Poulain
    I think subclassing QNetworkAccessManager will do the trick, I'll give that a shot.

  • I'm still unable to find a solution to this issue.

    unsupportedContent is actually firing, I had not setForwardUnsupportedContent to True. I've dumped the QNetworkRequest and QNetworkReply from QNetworkAccessManager.createRequest in an effort to understand what's going on:-

    createRequest: request - url - http://localhost:8002/Test/
    createRequest: request - raw header - User-Agent Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US) AppleWebKit/533.3 (KHTML, like Gecko) Qt/4.7.2 Safari/533.3
    createRequest: request - raw header - Accept application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,/;q=0.5

    createRequest: reply - url - http://localhost:8002/Test/
    createRequest: reply - attribute - QNetworkRequest.ConnectionEncryptedAttribute - false

    When unsupportedContent fires I dump the reply again:-

    onUnsupportedContent: reply - url - http://localhost:8002/Test/
    onUnsupportedContent: reply - attribute - QNetworkRequest.HttpStatusCodeAttribute - 200
    onUnsupportedContent: reply - attribute - QNetworkRequest.HttpReasonPhraseAttribute - OK
    onUnsupportedContent: reply - attribute - QNetworkRequest.ConnectionEncryptedAttribute - false
    onUnsupportedContent: reply - attribute - QNetworkRequest.HttpPipeliningWasUsedAttribute - false
    onUnsupportedContent: reply - raw header - Connection keep-alive
    onUnsupportedContent: reply - raw header - Transfer-Encoding chunked

    I'm inexperienced with this kind of thing, but I can't see anything wrong with this output. Am I missing something?

    @Benjamin Poulain
    I've not been able to force the content type by subclassing QNetworkAccessManager. Do you have experience doing this? If so, how did you do it?

  • Can't you wrap the QNetworkReply and change the headers?

  • Hi Benjamin,

    Thanks for the quick reply!

    If I try something like the following:-
    reply.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, 'text/html')

    I get the error message:-
    RuntimeError: no access to protected functions or signals for objects not created from Python

    Is this a limitation of PyQt?


  • Oh, I see. I was not aware python could not do that.
    I guess a solution would be to load the page separately and use QWebFrame::setHtml() but that could get messy with the history, referer, etc.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.