Save and load QWebEngineHistory to a QWebEnginePage - qt

I need to save the history of a QWebEnginePage and load it back. Therefore I want to store the history from page A in some structure and set it to page B.
In the documentation I found the following methods:
// Saves the web engine history history into stream.
QDataStream &operator<<(QDataStream &stream, const QWebEngineHistory &history)
// Loads the web engine history from stream into history.
QDataStream &operator>>(QDataStream &stream, QWebEngineHistory &history)
But honestly I don't know how to work with them.
I tried the following:
QWebEnginePage *m_history;
...
...
void setHistory(QWebEngineHistory *history){
QDataStream data;
data << history; //Hoping that the content of data is persistent after deleting of the QWebEnginePage where the history is coming from
data >> m_history;
}
And later on I want to load it back to the page:
m_history >> m_webEnginePage.history(); // Pseudo-Code
I know that the QWebEngineHistory of a QWebEnginePage is const, but then I'm wondering why are there even those two methods from above? Why is there a function that "loads the web engine history into history"?
The only alternative I can think of is storing my history in a QList, but managing this is not nice and could lead to more problems (because of the whole forward/backward button etc).
Thank you very much for your help.

No object can be saved, what is saved is the information associated with the object so you should not create QWebEngineHistory but save and/or load the information.
In the following example, the information is saved in a file when the application is closed and the startup is loaded.
#include <QtWebEngineWidgets>
int main(int argc, char *argv[]) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc,argv);
const QString filename = "history.bin";
QWebEngineView view;
view.load(QUrl("https://stackoverflow.com"));
{// load
QFile file(filename);
if(file.open(QFile::ReadOnly)){
qDebug() << "load";
QDataStream ds(&file);
ds >> *(view.page()->history());
}
}
view.resize(640, 480);
view.show();
int ret = app.exec();
{// save
QFile file(filename);
if(file.open(QFile::WriteOnly)){
qDebug() << "save";
QDataStream ds(&file);
ds << *(view.page()->history());
}
}
return ret;
}
In the same way you can save it through QSettings:
#include <QtWebEngineWidgets>
int main(int argc, char *argv[]) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc,argv);
QWebEngineView view;
view.load(QUrl("https://stackoverflow.com"));
{// load
QSettings settings;
QByteArray ba = settings.value("page/history").toByteArray();
QDataStream ds(&ba, QIODevice::ReadOnly);
ds >> *(view.page()->history());
}
view.resize(640, 480);
view.show();
int ret = app.exec();
{// save
QSettings settings;
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
ds << *(view.page()->history());
settings.setValue("page/history", ba);
}
return ret;
}

Related

How to display dynamically created images in QWebView?

I'm working with QtWebKit where I'm displaying dynamically created HTML content, and I need to display the images that I retrieve from the database. For example, if I need to display an image from the resource, I add this line to the content I use in QWebView::setHtml:
<img src="qrc:/images/image.jpg"/>
That works great, WebView automatically finds the resource and displays it. Now I need to substitute this image with the content that I retrieve from the database, and that doesn't have a file in the filesystem. How to do that?
Can I manage the qrc namespace adding the content dynamically? Is that possible to add my own QRC handler that would receive and serve the requests from WebView? If not QRC, is there any other protocol that I can use to provide the content to the images in WebView?
I have full control over what I'm adding to the WebView using the setHtml method.
Update: I would like to solve the same problem for QWebEngineView as well.
QtWebkit
In the case of QtWebkit you must intercept the request using a QNetworkAccessManager and resend a custom QNetworkReply.
#include <QtWebKitWidgets>
class CustomReply : public QNetworkReply{
public:
explicit CustomReply(const QByteArray & content, const QByteArray & contentType, const QUrl & url):
QNetworkReply(), m_content(content){
offset = 0;
setUrl(url);
open(ReadOnly | Unbuffered);
setHeader(QNetworkRequest::ContentTypeHeader, QVariant(contentType));
setHeader(QNetworkRequest::ContentLengthHeader, QVariant(m_content.size()));
QTimer::singleShot(0, this, &CustomReply::dispatch);
}
bool isSequential() const{
return true;
}
qint64 bytesAvailable() const{
return m_content.size() - offset + QIODevice::bytesAvailable();
}
public slots:
void abort(){
}
protected:
qint64 readData(char *data, qint64 maxSize){
if (offset < m_content.size()) {
qint64 number = qMin(maxSize, m_content.size() - offset);
::memcpy(data, m_content.constData() + offset, number);
offset += number;
return number;
} else
return -1;
}
private:
void dispatch(){
emit readyRead();
emit finished();
}
QByteArray m_content;
qint64 offset;
};
class NetworkAccessManager: public QNetworkAccessManager{
public:
using QNetworkAccessManager::QNetworkAccessManager;
protected:
QNetworkReply *createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData){
qDebug() << request.url();
if (request.url() == QUrl("qrc:/images/image.jpg")){
QImage image(150, 150, QImage::Format_RGB32);
image.fill(QColor("salmon"));
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "JPEG");
return new CustomReply(ba, "image/jpeg", request.url());
}
return QNetworkAccessManager::createRequest(op, request, outgoingData);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWebView view;
view.resize(640, 480);
view.show();
view.page()->setNetworkAccessManager(new NetworkAccessManager);
QString html = R"(<img src="qrc:/images/image.jpg">)";
view.setHtml(html);
return a.exec();
}
QtWebEngine
In QtWebEngine you must implement a QWebEngineUrlSchemeHandler but you cannot use the qrc, http or https schemas:
#include <QtWebEngineWidgets>
#define SCHEMENAME "so"
class Handler : public QWebEngineUrlSchemeHandler{
public:
void requestStarted(QWebEngineUrlRequestJob *job){
if(job->requestUrl() == QUrl("so:/images/image.jpg")){
QImage image(150, 150, QImage::Format_RGB32);
image.fill(QColor("salmon"));
QBuffer *buffer = new QBuffer;
buffer->open(QIODevice::WriteOnly);
image.save(buffer, "JPEG");
buffer->seek(0);
buffer->close();
job->reply("image/jpeg", buffer);
}
}
static void registerUrlScheme(){
QWebEngineUrlScheme webUiScheme(SCHEMENAME);
webUiScheme.setFlags(QWebEngineUrlScheme::SecureScheme |
QWebEngineUrlScheme::LocalScheme |
QWebEngineUrlScheme::LocalAccessAllowed);
QWebEngineUrlScheme::registerScheme(webUiScheme);
}
};
int main(int argc, char *argv[])
{
Handler::registerUrlScheme();
QApplication a(argc, argv);
QWebEngineView view;
Handler handler;
view.page()->profile()->installUrlSchemeHandler(SCHEMENAME, &handler);
view.resize(640, 480);
view.show();
QString html = R"(<img src="so:/images/image.jpg">)";
view.setHtml(html);
return a.exec();
}

QtMessageHandler / QTextStream causes memory usage to go up?

I have installed a simple QtMessageHandler in my application:
void handleMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QString text = msg;
QString path = QCoreApplication::applicationDirPath() + "/logging/logs.txt";
QFile logFile(path);
logFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream textStream(&logFile);
textStream<< text << endl << flush;
}
int main(int argc, char *argv[])
{
qInstallMessageHandler(handleMessage);
// ...
for (int i = 0; i < 10000000; i++) {
qDebug() << "Some text";
}
// ...
}
Now when I use qDebug to write some logs to the log file (and the application is doing nothing else!), the memory usage is shooting up. When I stop writing logs, it goes back down, but never to it's previous value. For every few thousand messages, the memory goes up a few hundred kilobytes and stays that way.
I think it's the QTextStream that is causing this, because if I comment that part out, then the memory usage just remains low.
I tried moving the myMessageHandler function to a separate class and have the QTextStream as a member variable, instead of creating it every time that myMessageHandler() is called, but then I couldn't get qInstallMessageHandler() to work.
int main(int argc, char *argv[])
{
MyMessageHandler myMessageHandler;
qInstallMessageHandler(myMessageHandler.handleMessage);
}
Which gives me:
cannot convert 'MyMessageHandler::handleMessage' from type 'void (MyMessageHandler::)(QtMsgType, const QMessageLogContext&, const QString&)' to type 'QtMessageHandler {aka void (*)(QtMsgType, const QMessageLogContext&, const QString&)}'qInstallMessageHandler(myMessageHandler.handleMessage);
Anyone have an idea on how to solve this?

QNetworkManager uploading file to FTP crash

I am trying to upload a simple test text file to a FTP server. In order to achieve this I am using QNetworkAccessManager, since QFtp has been deprecated in Qt 5.1.
I created a test.txt file in the programs directory and using QFile I am opening it as QIODevice::ReadWrite | QIODevice::Text.
The problem is when I set the connection and tell the QNetworkAccessManager to upload a file the program crashes ("FTPConnectionTest does not respond"). It happens both when I am trying to use an external FTP server or a local one created with FileZilla.
I connected all signals emitted by the reply (functions: uploadFinish, uploadProgress, uploadError) however no feedback is beeing captured.
Question: Is this problem lying on the side of FTP server or am I doing something wrong in my code?
Code snipped below:
Main.cpp
#include <QCoreApplication>
#include <ftp.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Ftp ftp;
return a.exec();
}
ftp.cpp
#include "ftp.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QFile>
#include <QUrl>
#include <QDebug>
Ftp::Ftp()
{
QFile file("test.txt");
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
url = QUrl("ftp://127.0.0.1/test.txt");
url.setUserName("user");
url.setPassword("password");
qDebug() << "URL set" << url;
QNetworkAccessManager* nam = new QNetworkAccessManager();
qDebug() << "nam set";
QNetworkReply *rep = nam->put(QNetworkRequest(url), &file);
qDebug() << "after rep";
connect(rep, SIGNAL(finished()), this, SLOT(uploadFinish()));
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(uploadError(QNetworkReply::NetworkError)));
connect(rep, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(uploadProgress(qint64,qint64)));
}
else qDebug() << "failed to open";
}
void Ftp::uploadFinish()
{
qDebug() << "finished uploading file";
}
void Ftp::uploadProgress(qint64 a, qint64 b)
{
qDebug() << a << "/" << b;
}
void Ftp::uploadError(QNetworkReply::NetworkError state)
{
qDebug() << "State" << state;
}
See the QNetworkAccessManager::put documentation:
data must be opened for reading when this function is called and must remain valid until the finished() signal is emitted for this reply.
Your file object falls out of scope when the constructor finishes execution, so QNetworkAccessManager probably tries to read from object that is already deleted. You need to make file a class member variable or create it using QFile* file = new QFile().

QT QString from QDataStream

I'm working with a buffer and I'm trying to get a string from it, but isnt working...
Example:
*void myFunc(QDataStream& in)
{
quint8 v;
in >> v;
// Ok, I caught v value successfuly
QString s;
in >> s;
// Didnt work :<
}*
The string lenght is stored on 2 first bytes...
Thanks
If the string was not written as a QString, you need to read its length and content separately.
quint8 v;
in >> v;
quint16 length = 0;
in >> length;
// the string is probably utf8 or latin
QByteArray buffer(length, Qt::Uninitialized);
in.readRawData(buffer.data(), length);
QString string(buffer);
You might have to change the endianness of the QDataStream with QDataStream::setByteOrder before reading the 16-bit length.
We should really see the writing code and how you create the QDataStream. I tried with the following sample, and in this case your function works very well:
#include <QCoreApplication>
#include <QDebug>
#include <QDataStream>
#include <QBuffer>
void myFunc(QDataStream& in)
{
quint8 v;
in >> v;
qDebug() << v;
// Ok, I caught v value successfuly
QString s;
in >> s;
qDebug() << s;
// Didnt work :<
}
int main(int argc, char ** argv) {
QCoreApplication a(argc, argv);
QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
// write test data into the buffer
QDataStream out(&buffer);
quint8 ival = 42;
QString sval = "Qt";
out << ival;
out << sval;
// read back data
buffer.seek(0);
myFunc(out);
return a.exec();
}
Output when executed:
$ ./App
42
"Qt"

Serializing QHash to QByteArray

I am trying to serialize a QHash object and store it in a QByteArray (to be sent using QUDPSocket or QTCPSocket).
My current attempt looks like this:
// main.cpp
#include <QtCore/QCoreApplication>
#include <QHash>
#include <QVariant>
#include <QDebug>
int main(int argc, char *argv[])
{
QHash<QString,QVariant> hash;
hash.insert("Key1",1);
hash.insert("Key2","thing2");
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
ds << hash;
qDebug() << ds;
}
When this runs I get this out of qDebug():
QIODevice::read: WriteOnly device
QIODevice::read: WriteOnly device
QIODevice::read: WriteOnly device
QVariant(, )
The documentation says that this should write to the byte array, but obviously that isn't happening here. What am I doing wrong?
Qt 4.7.1 on OS-X
Thanks!
-J
The reason it is failing is because it is trying to read from a write-only stream. The sequence is:
qDebug() << ds;
--> QVariant::QVariant(QDataStream &s)
--> QDataStream& operator>>(QDataStream &s, QVariant &p)
--> void QVariant::load(QDataStream &s)
That last method (and some more downstream) try to read from the data stream to convert its contents into a QVariant for display in qDebug. In other words, your actual code is fine; the debugging check is causing the failure.
You could check the contents of the byte array with something like:
qDebug() << ba.length() << ba.toHex();
You can Implement you program like this code:
QHash<QString,QVariant> options;
options["string"] = "my string";
options["bool"] = true;
QByteArray ar;
//Serializing
QDataStream out(&ar,QIODevice::WriteOnly); // write the data
out << options;
//setting a new value
options["string"] = "new string";
//Deserializing
// read the data serialized from the file
QDataStream in(&ar,QIODevice::ReadOnly);
in >> options;
qDebug() << "value: " << options.value("string");
ref

Resources