#include <QNetworkReply> #include <QWebSecurityOrigin>#include <qcoreapplication.h>#include <QtCore/QMetaObject>class ProxyNetworkReply: public QNetworkReply{ Q_OBJECTpublic: ProxyNetworkReply(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op) : QNetworkReply(parent) { setRequest(req); setUrl(req.url()); setOperation(op); setFinished(true); QNetworkReply::open(QIODevice::ReadOnly | QIODevice::Unbuffered); QString string = "<!DOCTYPE HTML><html> <head> </head> <body><H1> description</H1></body></html>"; m_content = string.toUtf8(); m_lOffset = 0; setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/html; charset=UTF-8")); setHeader(QNetworkRequest::ContentLengthHeader, QVariant(m_content.size())); QMetaObject::invokeMethod(this, "metaDataChanged", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "downloadProgress", Qt::QueuedConnection, Q_ARG(qint64, m_content.size()), Q_ARG(qint64, m_content.size())); QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "readChannelFinished", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); QCoreApplication::postEvent(this, new QEvent(QEvent::NetworkReplyUpdated)); } void abort() { QNetworkReply::close(); } qint64 bytesAvailable() const { return m_content.size() - m_lOffset; } bool isSequential() const { return true; } qint64 size() const { return m_content.size(); }protected: qint64 readData(char* pData, qint64 lMaxSize) { if (m_lOffset >= m_content.size()) return -1; qint64 lCount = qMin(lMaxSize, m_content.size() - m_lOffset); memcpy(pData, m_content.constData() + m_lOffset, lCount); m_lOffset += lCount; return lCount; }private slots:private: QByteArray m_content; qint64 m_lOffset;};
#include <QNetworkReply>#include <qcoreapplication.h>#include <QtCore/QMetaObject>class ProxyNetworkReply: public QNetworkReply{ Q_OBJECTpublic: ProxyNetworkReply(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op) : QNetworkReply(parent) { setRequest(req); setUrl(req.url()); setOperation(op); } void abort() { QNetworkReply::close(); } qint64 bytesAvailable() const { return m_content.size() - m_lOffset; } bool isSequential() const { return true; } qint64 size() const { return m_content.size(); } void setData(QString string) { setFinished(true); QNetworkReply::open(QIODevice::ReadOnly | QIODevice::Unbuffered); //QString string = "<!DOCTYPE HTML><html> <head> </head> <body><H1> description</H1></body></html>"; m_content = string.toUtf8(); m_lOffset = 0; setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/html; charset=UTF-8")); setHeader(QNetworkRequest::ContentLengthHeader, QVariant(m_content.size())); QMetaObject::invokeMethod(this, "metaDataChanged", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "downloadProgress", Qt::QueuedConnection, Q_ARG(qint64, m_content.size()), Q_ARG(qint64, m_content.size())); QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "readChannelFinished", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); QCoreApplication::postEvent(this, new QEvent(QEvent::NetworkReplyUpdated)); } protected: qint64 readData(char* pData, qint64 lMaxSize) { if (m_lOffset >= m_content.size()) return -1; qint64 lCount = qMin(lMaxSize, m_content.size() - m_lOffset); memcpy(pData, m_content.constData() + m_lOffset, lCount); m_lOffset += lCount; return lCount; }private slots:private: QByteArray m_content; qint64 m_lOffset;};