[PyQt] PyQt5 and QQuickImageProvider

Phil Thompson phil at riverbankcomputing.com
Mon Oct 7 14:33:38 BST 2013


On Mon, 7 Oct 2013 00:09:51 +0200, Jens Persson <xerxes2 at gmail.com> wrote:
> 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?

The bug is in PyQt4. The size should be returned and not passed in as an
argument.

In your PyQt5 code change the return statement to something like...

    return pixmap, QtCore.QSize(100, 100)

I probably won't fix the bug at this stage as it would break existing
code.

Phil


More information about the PyQt mailing list