Getting the QQuickImageProvider Example run in PySide6
Unsolved
Qt for Python
-
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" } }
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())
-
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.