Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 to QtCore.QObject instead of the declared QQmlImageProviderBase 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?


  • Lifetime Qt Champion

    Hi,

    @xtofl said in How to create an ImageProvider with PySide6?:

    QQmlImageProviderBase

    Shouldn't you be using QQmlImageProvider as your class base ?


  • Lifetime Qt Champion

    Hi,

    @xtofl said in How to create an ImageProvider with PySide6?:

    QQmlImageProviderBase

    Shouldn't you be using QQmlImageProvider as your class base ?



  • @SGaist said in How to create an ImageProvider with PySide6?:

    QQmlImageProviderBase

    That was it, thanks!

    I'm still curious about what the error message meant, though. Lots to learn!


  • Lifetime Qt Champion

    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 the QObject to be a base?

    super() returns a class unrelated to QObject, but super().__init__ requires its subclasses to be? Is it intended to inherit 'sideways'?

    Is it allright to be confused here?



  • @xtofl

    super().__init__(parent)
    

    I think it is complaining/picking up the QObject from parent: QtCore.QObject. What QQmlImageProviderBase accepts a QObject in its constructor? None, and it has no constructor. Remove the parent actual parameter and see what the error message reads?



  • @JonB What makes you think that?

    Anyhow, behaviour is the same without the parent argument. PySide6.QtCore.QObject isn't a direct base class of GenericImageProvider.


Log in to reply