Python QQuickImageProvider PyQt5

Python QQuickImageProvider PyQt5,python,qml,pyqt5,Python,Qml,Pyqt5,我尝试使用QQueGimeApvices发送QMIQUE到QML,所有的工作都在C++ QT5.5.2中进行,但是我尝试与PyQT55.5.2类似的代码,QML只是表示错误:IMAGE提供程序支持图像类型,但没有实现ReQuimTime,但实际上,我实现了ReqMimTime,这里我的代码: main.py: example.qml: 也许在Python和C++中,ReQuimTime有不同的参数和返回值,我确信格式是正确的。参考一些例子,我不知道我是怎么了 根据我正在审查的问题是QQuickV

我尝试使用QQueGimeApvices发送QMIQUE到QML,所有的工作都在C++ QT5.5.2中进行,但是我尝试与PyQT55.5.2类似的代码,QML只是表示错误:IMAGE提供程序支持图像类型,但没有实现ReQuimTime,但实际上,我实现了ReqMimTime,这里我的代码:

main.py:

example.qml:


也许在Python和C++中,ReQuimTime有不同的参数和返回值,我确信格式是正确的。参考一些例子,我不知道我是怎么了

根据我正在审查的问题是QQuickView或QQmlEngine,这些类已经过时了

我建议您使用QQmlApplicationEngine:

main.py

example.qml


我也有同样的问题。似乎是因为提供程序实例在传递到addImageProvider后丢失了。我像下面这样解决了这个问题

app = QGuiApplication([])    

viewer = QQuickView()
provider = MyImageProvider() # keep this instance during your app running
viewer.engine().addImageProvider("myprovider", provider)
viewer.setResizeMode(QQuickView.SizeRootObjectToView)
viewer.setSource(QUrl("example.qml"))
viewer.show()

app.exec()
import QtQuick 2.7

Item {
    id: root
    width: 800
    height: 600
    Image{
         // width: 300
         // height: 300
         source: "image://myprovider/test.png"
    }
}
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtQml import *
from PyQt5.QtQuick import *


class MyImageProvider(QQuickImageProvider):
    def __init__(self):
        super(MyImageProvider, self).__init__(QQuickImageProvider.Image)

    def requestImage(self, p_str, size):
        img = QImage(300, 300, QImage.Format_RGBA8888)
        img.fill(Qt.red)
        return img, img.size()


if __name__ == '__main__':
    app = QGuiApplication(sys.argv)

    engine = QQmlApplicationEngine()
    engine.addImageProvider("myprovider", MyImageProvider())
    engine.load(QUrl.fromLocalFile("example.qml"))
    if len(engine.rootObjects()) == -1:
        sys.exit(-1)
    sys.exit(app.exec_())
import QtQuick 2.7
import QtQuick.Window 2.2

Window{
    visible: true
    width: 640
    height: 480
    Image{
         anchors.fill : parent
         source: "image://myprovider/test.png"
    }
}
app = QGuiApplication([])    

viewer = QQuickView()
provider = MyImageProvider() # keep this instance during your app running
viewer.engine().addImageProvider("myprovider", provider)
viewer.setResizeMode(QQuickView.SizeRootObjectToView)
viewer.setSource(QUrl("example.qml"))
viewer.show()

app.exec()