How to create an ImageProvider with PySide6?
-
I'm following http://www.lothlorien.com/kf6gpe/?p=234 to get an image into my Qml page. Simply put, the Qml would 'request' an image via the image provider, which is registered to the qml engine by the python backend:
def main(): ... engine = QtQml.QQmlApplicationEngine() engine.addImageProvider("mine", GenericImageProvider(random_image)) engine.load(QtCore.QUrl.fromLocalFile(str(main_file))) ...
class GenericImageProvider(QtQml.QQmlImageProviderBase): def __init__(self, get_image: Callable[[], QtGui.QImage], parent: QtCore.QObject = None): super().__init__(parent) self._get_image = get_image def requestImage(self, id_: str, size: QtCore.QSize) -> QtGui.QImage: return self._get_image().scaled(size) ...
Now I'm not getting past the initialization of my provider, and I have no clue what I'm doing wrong:
File "ui.py", line 36, in __init__ super().__init__(parent) TypeError: PySide6.QtCore.QObject isn't a direct base class of GenericImageProvider
It seems as though the
super()
call directly resolves toQtCore.QObject
instead of the declaredQQmlImageProviderBase
base class.With plain python objects, this behaves like expected:
>>> class Base: ... def __init__(self, p): ... print('initializing ' + p) ... >>> >>> class Derived(Base): ... def __init__(self, q): ... super().__init__(q + ' from derived') ... >>> >>> d = Derived('d') initializing d from derived
What am I doing wrong?
-
Hi,
@xtofl said in How to create an ImageProvider with PySide6?:
QQmlImageProviderBase
Shouldn't you be using QQmlImageProvider as your class base ?
-
If you take a look at that class implementation, you'll see that it is not a QObject based class and even more it has no constructor.
-
@SGaist any idea why
super().__init__(parent)
ends up in a place where it requires theQObject
to be a base?super()
returns a class unrelated toQObject
, butsuper().__init__
requires its subclasses to be? Is it intended to inherit 'sideways'?Is it allright to be confused here?
-
super().__init__(parent)
I think it is complaining/picking up the
QObject
fromparent: QtCore.QObject
. WhatQQmlImageProviderBase
accepts aQObject
in its constructor? None, and it has no constructor. Remove theparent
actual parameter and see what the error message reads?