Using QTWebKit to display a website stored in memory

余生长醉 提交于 2020-01-02 04:31:28

问题


Currently I have my HTML, JS, CSS, graphics, etc stored locally on hard disk and access them using QWebFrame::SetUrl( QUrl::fromLocalFile( "appFolder\html\index.html" )). At some point I am going to need to encrypt the locally stored files so I'm looking for a way to either decrypt them as they're requested or to decrypt them all into memory and access them that way.

I know I can use QWebFrame::setContent( htmlData ) to load the HTML from memory so I can load the encrypted HTML file, decrypt it in memory and then display it that way, but how would I go about the other data (JS, CSS, graphics, etc) which is currently stored in subfolders?

Alternatively, is there a way I can intercept requests for access to all the HTML, JS, CSS, etc files and decrypt them as they're loaded?

By using my own NetworkAccessManager I can intercept calls to createRequest so I can see when each file is being loaded, but I can't see how to use this to decrypt the data on the fly. I can also connect a slot function to the finished(QNetworkReply*) signal, but at that point the data has already been read - the QIODevice's current position is pointing to the end of the file.

I'd be very grateful for any advice or pointers in the right direction.


回答1:


I think in your case the best solution is to inherit QNetworkReply class and use this new class in reimplemented QNetworkAccessManager::createRequest() function.

In general, you should reimplement next virtual functions of QNetworkReply: bytesAvailable(), readData(char *data, qint64 maxSize), close(), abort().

For example, readData should be the folowing:

qint64 NetworkReplyEx::readData(char *data, qint64 maxSize)
{
    return m_buffer.read(data, maxSize);
}

where m_buffer is already decrypted data.

Also you need to add all necessary logic in this class to get encrypted data, decrypt this data... In the end you should manually emit finished() signal inside new class, so QWebView or other related class will get decrypted html.



来源:https://stackoverflow.com/questions/6427340/using-qtwebkit-to-display-a-website-stored-in-memory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!