[PyQt] PyQt5 and QQuickImageProvider

Jens Persson xerxes2 at gmail.com
Sun Oct 6 23:09:51 BST 2013


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:
---
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5 import QtQml
from PyQt5 import QtQuick

class ImageProviderGUI(QtCore.QObject):
    def __init__(self):
        QtCore.QObject.__init__(self)
        self.app = QtWidgets.QApplication(["ImageProvider"])
        self.view = QtQuick.QQuickView()
        self.view.setResizeMode(QtQuick.QQuickView.SizeRootObjectToView)
        self.context = self.view.rootContext()
        engine = self.context.engine()
        self.image_provider = ImageProvider()
        engine.addImageProvider("cover", self.image_provider)
        self.view.setSource(QtCore.QUrl("test.qml"))
        self.view.show()
        self.app.exec_()

class ImageProvider(QtQuick.QQuickImageProvider):
    def __init__(self):
        QtQuick.QQuickImageProvider.__init__(self,
QtQuick.QQuickImageProvider.Pixmap)

    def requestPixmap(self, id, size):
        pixmap = QtGui.QPixmap(100, 100)
        pixmap.fill(QtGui.QColor(id))
        return pixmap

ImageProviderGUI()
---
import QtQuick 2.0

Image {
        source: "image://cover/red"
}
---
The code above gives an error:
---
TypeError: invalid result type from ImageProvider.requestPixmap()
file:///home/xerxes2/test.qml:4:1: QML Image: Failed to get image from
provider: image://cover/red
---
The same thing works just fine with PyQt4:
---
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4 import QtDeclarative

class ImageProviderGUI(QtCore.QObject):
    def __init__(self):
        QtCore.QObject.__init__(self)
        self.app = QtGui.QApplication(["ImageProvider"])
        self.view = QtDeclarative.QDeclarativeView()

self.view.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView)
        self.context = self.view.rootContext()
        engine = self.context.engine()
        self.image_provider = ImageProvider()
        engine.addImageProvider("cover", self.image_provider)
        self.view.setSource(QtCore.QUrl("test1.qml"))
        self.view.show()
        self.app.exec_()

class ImageProvider(QtDeclarative.QDeclarativeImageProvider):
    def __init__(self):
        QtDeclarative.QDeclarativeImageProvider.__init__(self,
QtDeclarative.QDeclarativeImageProvider.Pixmap)

    def requestPixmap(self, id, size, requestedSize):
        pixmap = QtGui.QPixmap(100, 100)
        pixmap.fill(QtGui.QColor(id))
        return pixmap

ImageProviderGUI()
---
import QtQuick 1.1

Image {
        source: "image://cover/red"
}
---
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?

Greets Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20131007/f78caa7c/attachment.html>


More information about the PyQt mailing list