qmlRegisterTypeSingleton with contextData error while using PySide6
Unsolved
QML and Qt Quick
-
I've been having this issue for a long time now. The existing code worked fine for years and didn't start failing until after a QT update over six months ago. If you register a singleton using PySide6, you will get a 'Cannot assign object to list property "contextData"' error message when the QML code has a ListModel. This happens even if the singleton is not being used by the QML code. If you remove qmlRegisterSingletonType, the error goes away. I've searched the internet and have not seen this error happening due to adding a singleton, so I'm not sure what's going on.
# singleton.py from PySide6.QtCore import Qt from PySide6.QtCore import QObject class Singleton(QObject): def __init__(self): super().__init__()
# app import sys from PySide6.QtCore import Qt, QObject, QAbstractListModel, QUrl, QByteArray, Signal, Slot, Property from PySide6.QtGui import QGuiApplication from PySide6.QtQml import QQmlApplicationEngine from PySide6 import QtQml from singleton import Singleton _singleton = Singleton() def singleton_callback(self): return _singleton if __name__ == "__main__": app = QGuiApplication(sys.argv) engine = QQmlApplicationEngine() # This Fails QtQml.qmlRegisterSingletonType(Singleton, 'Test', 1, 0, 'Singleton',singleton_callback) # This Works #QtQml.qmlRegisterType(Singleton, 'Test', 1, 0, 'Singleton') engine.quit.connect(app.quit) engine.load('main.qml') sys.exit(app.exec())
// main.qml import QtQuick import QtQuick.Controls import Test 1.0 ApplicationWindow { id: main width: 450 height: 450 title: "Singleton Test" visible: true ListModel { id: myModel } Component.onCompleted: {} }
-
Hi,
Do you have the same issue with PySide6 ?
-
Sorry, I misread something and thought you were using PyQt6...
Which version of PySide6 are you using ?
How did you install it ?