December 30, 2011

cancel cancel
Lab Rat
2 posts

[solved] Cannot add custom url scheme handler to QWebView

 

I’m trying to add support of custom URLs to QWebView. I’ve found tutorial that explains this in details: create new class derived from QNetworkAccessManager, create new QNetworkReply-derived class etc. Ok, I followed that guide and wrote simple project, but for some reasons QWebView doesn’t display data I’m sending back in the reply, just white empty page. My point is I already have all needed data for custom url at the moment of Reply instance creation, and I don’t perform any actual network requests.

The simple demo project I created is available here [dl.dropbox.com]

From debug marks I see that my data successfully sent back to caller, but webpage displays nothing.

Here are some code fragments:

  1. Reply::Reply(const QUrl & url)
  2.     : QNetworkReply()
  3. {
  4.     qDebug() << "gen reply";
  5.     open(ReadOnly | Unbuffered);
  6.  
  7.     content = QByteArray('t', 1000); // fill with character 't'
  8.     setHeader(QNetworkRequest::ContentTypeHeader,
  9.                     QVariant("text/html; charset=UTF-8"));
  10.     setHeader(QNetworkRequest::ContentLengthHeader,
  11.                     QVariant(content.size()));
  12.  
  13.     offset = 0;
  14.  
  15.     QTimer::singleShot(0, this, SIGNAL(metaDataChanged()));
  16.     QTimer::singleShot(0, this, SIGNAL(readyRead()));
  17.     QTimer::singleShot(0, this, SIGNAL(finished()));
  18.  
  19. }
  20.  
  21. qint64 Reply::readData(char *data, qint64 maxSize)
  22. {
  23.     if (offset < content.size()) {
  24.         qint64 number = qMin(maxSize, content.size() - offset);
  25.         memcpy(data, content.constData() + offset, number);
  26.         qDebug() << "fetching content";
  27.         offset += number;
  28.         return number;
  29.     } else
  30.         return -1;
  31. }

1 reply

December 30, 2011

cancel cancel
Lab Rat
2 posts

I’ve found cause of the problem: missing call of parent bytesAvailable, so correct method definition must looks like this:

  1. qint64 Reply::bytesAvailable() const
  2. {
  3.     qint64 bc = QIODevice::bytesAvailable() + content.size() - offset;
  4.  
  5.     qDebug() << "Reply::bytesAvailable()" << bc;
  6.     return bc;
  7. }

 
  ‹‹ Does QtWebkit 2.2.1 fix the HTML5 media support problem?      Can build qtwebkit2.2 use QtCreator in QtSDK4.8 ? ››

You must log in to post a reply. Not a member yet? Register here!