PyQt6: can't serialize QWebEngineHistory
Phil Thompson
phil at riverbankcomputing.com
Mon Nov 22 16:07:13 GMT 2021
On 22/11/2021 06:08, jimmy at spalge.com wrote:
> I don't seem to be able to serialize QWebEngineHistory objects to a
> QDataStream anymore with PyQt (and Qt) 6.2.0 and 6.2.1. A warning log
> message
> is printed out and the data steam just has a few null bytes. The
> message is
> `QVariant::save: unable to save type 'QObject*' (type id: 39)`. It
> seems
> QVariant isn't seeing the right type? Here are a couple of example
> programs in
> python and c++ and their output to demonstrate.
>
> Python:
>
> try:
> from PyQt6.QtCore import QDataStream, QByteArray, QIODevice,
> QVariant, QUrl
> from PyQt6.QtWebEngineWidgets import QWebEngineView
> from PyQt6.QtWidgets import QApplication
> except ImportError:
> from PyQt5.QtCore import QDataStream, QByteArray, QIODevice,
> QVariant, QUrl
> from PyQt5.QtWebEngineWidgets import QWebEngineView
> from PyQt5.QtWidgets import QApplication
>
> def serialize(history):
> data = QByteArray()
> stream = QDataStream(data, QIODevice.OpenModeFlag.WriteOnly)
> stream << history
> print(f"bytes: {len(data)}")
> print(f"type: {history}")
> print(f"QVariant type: {QVariant(history).typeName()}")
>
> app = QApplication([])
> view = QWebEngineView()
> view.loadFinished.connect(lambda: serialize(view.history()))
> view.load(QUrl("https://example.com/"))
> view.show();
> app.exec();
>
> PyQt5 (and Qt5) output:
>
> bytes: 498
> type: <PyQt5.QtWebEngineWidgets.QWebEngineHistory object at
> 0x7f785c8cb040>
> QVariant type: PyQt_PyObject
>
> PyQt6 (and Qt6) output:
>
> QVariant::save: unable to save type 'QObject*' (type id: 39).
> bytes: 5
> type: <PyQt6.QtWebEngineCore.QWebEngineHistory object at
> 0x7f2b7d79bb80>
> QVariant type: QObject*
>
> C++:
>
> #include <iostream>
> #include <QApplication>
> #include <QWebEngineView>
> #include <QWebEngineHistory>
> #include <QUrl>
> #include <QIODevice>
> #include <QObject>
> #include <QDebug>
>
> void serialize(QWebEngineHistory *history) {
> QByteArray data;
> QDataStream stream(&data, QIODevice::WriteOnly);
> stream << *history;
> qDebug() << "bytes: " << data.length();
> qDebug() << "type: " << history;
> }
>
> int main(int argc, char *argv[])
> {
> QApplication app(argc, argv);
> QWebEngineView view;
> QObject::connect(&view, &QWebEngineView::loadFinished,
> [&view]() {
> serialize(view.history());
> });
> view.load(QUrl("https://example.com/"));
> view.show();
> return app.exec();
> }
>
> C++ Qt6 output:
>
> bytes: 498
> type: QWebEngineHistory(0x560d9e171f80)
Should be fixed in the next snapshot.
Thanks,
Phil
More information about the PyQt
mailing list