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. Getting the QQuickImageProvider Example run in PySide6
Forum Update on Monday, May 27th 2025

Getting the QQuickImageProvider Example run in PySide6

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 521 Views
  • 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.
  • M Offline
    M Offline
    MaKaNu
    wrote on last edited by MaKaNu
    #1

    I have tried to implemented the QQuickImageProvider in our Application. and got prompted with the error:

    TypeError: 'PySide6.QtQml.QQmlEngine.addImageProvider' called with wrong argument types:
      PySide6.QtQml.QQmlEngine.addImageProvider(str, ObjectType)
    Supported signatures:
      PySide6.QtQml.QQmlEngine.addImageProvider(str, PySide6.QtQml.QQmlImageProviderBase)
    

    So I found following solved Post but it seems not to be the same issue I struggle with.

    I translated the given cpp code from the example to following python code:

    image_provider.py:

    from PySide6.QtQml import QQmlImageProviderBase
    from PySide6.QtQuick import QQuickImageProvider
    from PySide6.QtCore import QSize
    from PySide6.QtGui import QPixmap, QColor
    
    
    class ColorImageProvider(QQuickImageProvider):
        def __init__(
            self,
            type: QQmlImageProviderBase.ImageType,
            flags: QQmlImageProviderBase.Flags = ...,
        ) -> None:
            super().__init__(type, flags)
    
        def requestPixmap(self, id: str, size: QSize, requestedSize: QSize) -> QPixmap:
            width, height = 100, 50
    
            if size:
                size = QSize(width, height)
            pixmap = QPixmap(
                requestedSize.width() if requestedSize.width() > 0 else width,
                requestedSize.height() if requestedSize.height() > 0 else height,
            )
            pixmap.fill(QColor(id).rgba())
            return pixmap
    

    colors.qml:

    Column {
        Image { source: "image://colors/yellow" }
        Image { source: "image://colors/red" }
    }
    

    main.py:

    import sys
    from pathlib import Path
    
    from PySide6.QtQuick import QQuickView
    from PySide6.QtGui import QGuiApplication
    
    from image_provider import ColorImageProvider
    
    # To be used on the @QmlElement decorator
    # (QML_IMPORT_MINOR_VERSION is optional)
    QML_IMPORT_NAME = "io.qt.textproperties"
    QML_IMPORT_MAJOR_VERSION = 1
    
    if __name__ == "__main__":
        app = QGuiApplication(sys.argv)
        view = QQuickView()
        engine = view.engine()
        engine.addImageProvider("colors", ColorImageProvider)
        qml_file = Path(__file__).parent / "colors.qml"
        view.setSource(qml_file)
        view.show()
    
        if not engine.rootObjects():
            sys.exit(-1)
    
        sys.exit(app.exec())
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      addImageProvider expects an instance of your custom provider not the class.

      By the way, in both examples, the subclass have an empty constructor that just call the base class with QQuickImageProvider::Pixmap as parameter, I would implement something similar.

      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
      0
      • M Offline
        M Offline
        Momo007
        wrote on last edited by
        #3

        Hey ! did you find a solution to this Issue ? because the qt pyside 6 example seems to be broken

        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