<div dir="ltr"><div><div><div>Hi, I'm trying to port an app to PyQt5 and everything has been working smoothly so far ... except one thing. I can't get my QQuickImageProvider to work properly:<br>---<br>from PyQt5 import QtCore<br>
from PyQt5 import QtGui<br>from PyQt5 import QtWidgets<br>from PyQt5 import QtQml<br>from PyQt5 import QtQuick<br><br>class ImageProviderGUI(QtCore.QObject):<br> def __init__(self):<br> QtCore.QObject.__init__(self)<br>
self.app = QtWidgets.QApplication(["ImageProvider"])<br> self.view = QtQuick.QQuickView()<br> self.view.setResizeMode(QtQuick.QQuickView.SizeRootObjectToView)<br> self.context = self.view.rootContext()<br>
engine = self.context.engine()<br> self.image_provider = ImageProvider()<br> engine.addImageProvider("cover", self.image_provider)<br> self.view.setSource(QtCore.QUrl("test.qml"))<br>
self.view.show()<br> self.app.exec_()<br><br>class ImageProvider(QtQuick.QQuickImageProvider):<br> def __init__(self):<br> QtQuick.QQuickImageProvider.__init__(self, QtQuick.QQuickImageProvider.Pixmap)<br>
<br> def requestPixmap(self, id, size):<br> pixmap = QtGui.QPixmap(100, 100)<br> pixmap.fill(QtGui.QColor(id))<br> return pixmap<br><br>ImageProviderGUI()<br>---<br>import QtQuick 2.0<br><br>Image {<br>
source: "image://cover/red"<br>}<br>---<br></div><div>The code above gives an error:<br>---<br>TypeError: invalid result type from ImageProvider.requestPixmap()<br>file:///home/xerxes2/test.qml:4:1: QML Image: Failed to get image from provider: image://cover/red<br>
---<br></div>The same thing works just fine with PyQt4:<br>---<br>from PyQt4 import QtCore<br>from PyQt4 import QtGui<br>from PyQt4 import QtDeclarative<br><br>class ImageProviderGUI(QtCore.QObject):<br> def __init__(self):<br>
QtCore.QObject.__init__(self)<br> self.app = QtGui.QApplication(["ImageProvider"])<br> self.view = QtDeclarative.QDeclarativeView()<br> self.view.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView)<br>
self.context = self.view.rootContext()<br> engine = self.context.engine()<br> self.image_provider = ImageProvider()<br> engine.addImageProvider("cover", self.image_provider)<br> self.view.setSource(QtCore.QUrl("test1.qml"))<br>
self.view.show()<br> self.app.exec_()<br><br>class ImageProvider(QtDeclarative.QDeclarativeImageProvider):<br> def __init__(self):<br> QtDeclarative.QDeclarativeImageProvider.__init__(self, QtDeclarative.QDeclarativeImageProvider.Pixmap)<br>
<br> def requestPixmap(self, id, size, requestedSize):<br> pixmap = QtGui.QPixmap(100, 100)<br> pixmap.fill(QtGui.QColor(id))<br> return pixmap<br><br>ImageProviderGUI()<br>---<br>import QtQuick 1.1<br>
<br>Image {<br> source: "image://cover/red"<br>}<br>---<br></div>Also the requestPixmap method takes one parameter less in PyQt5. So something is not quite right ... and I can't figure out where the bug is? <br>
<br></div>Greets Jens<br></div>