[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":http://dl.dropbox.com/u/11629166/tmp/sample.zip

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

    Here are some code fragments:

    @Reply::Reply(const QUrl & url)
    : QNetworkReply()
    {
    qDebug() << "gen reply";
    open(ReadOnly | Unbuffered);

    content = QByteArray('t', 1000); // fill with character 't'
    setHeader(QNetworkRequest::ContentTypeHeader,
                    QVariant("text/html; charset=UTF-8"));
    setHeader(QNetworkRequest::ContentLengthHeader,
                    QVariant(content.size()));
    
    offset = 0;
    
    QTimer::singleShot(0, this, SIGNAL(metaDataChanged()));
    QTimer::singleShot(0, this, SIGNAL(readyRead()));
    QTimer::singleShot(0, this, SIGNAL(finished()));
    

    }

    qint64 Reply::readData(char *data, qint64 maxSize)
    {
    if (offset < content.size()) {
    qint64 number = qMin(maxSize, content.size() - offset);
    memcpy(data, content.constData() + offset, number);
    qDebug() << "fetching content";
    offset += number;
    return number;
    } else
    return -1;
    }
    @



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

    @qint64 Reply::bytesAvailable() const
    {
    qint64 bc = QIODevice::bytesAvailable() + content.size() - offset;

    qDebug() << "Reply::bytesAvailable()" << bc;
    return bc;
    

    }@


Log in to reply
 

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