PyQt6: can't serialize QWebEngineHistory

jimmy at spalge.com jimmy at spalge.com
Mon Nov 22 06:08:59 GMT 2021


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)



More information about the PyQt mailing list