Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to create an ImageProvider with PySide6?
Qt 6.11 is out! See what's new in the release blog

How to create an ImageProvider with PySide6?

Scheduled Pinned Locked Moved Solved Qt for Python
pyside
7 Posts 3 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xtofl
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      QQmlImageProviderBase

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • X Offline
        X Offline
        xtofl
        wrote on last edited by
        #3

        @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!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          X 1 Reply Last reply
          2
          • SGaistS SGaist

            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.

            X Offline
            X Offline
            xtofl
            wrote on last edited by
            #5

            @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?

            JonBJ 1 Reply Last reply
            0
            • X xtofl

              @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?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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?

              X 1 Reply Last reply
              0
              • JonBJ JonB

                @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?

                X Offline
                X Offline
                xtofl
                wrote on last edited by
                #7

                @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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved