from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtDeclarative import *

qmlFile = "test.qml"

class ColorImageProvider(QDeclarativeImageProvider):
    def __init__(self):
        super(ColorImageProvider, self).__init__(QDeclarativeImageProvider.Pixmap)
        #print "ColorImageProvider init"

    def requestPixmap(self, id, size, requestedSize):
        print "Pixmap ARGS", id, size, requestedSize
        return super(ColorImageProvider, self).requestPixmap(id, size, requestedSize)
        width = 100
        height = 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))
        print "RES",pixmap,size
        return pixmap

    def requestImage(self, id, size, requestedSize):
        print "Image ARGS", id, size, requestedSize
        return super(ColorImageProvider, self).requestImage(id, size, requestedSize)

import sys
app = QApplication([])
canvas = QDeclarativeView()
engine = canvas.engine()
engine.addImageProvider("colors", QDeclarativeImageProvider(QDeclarativeImageProvider.Pixmap))
engine.addImageProvider("colors2", ColorImageProvider())

canvas.setSource(QUrl.fromLocalFile(qmlFile))
engine.quit.connect(app.quit)

canvas.setGeometry(QRect(100, 100, 100, 100))
canvas.show()

sys.exit(app.exec_())    
