问题
I have created a class that inherits QQuickImageProvider Class,
but i want to use the requestImage()
function of QQuickImageProvider to set the QImage variable , but i dont know how to do that as i need that QImage variable from a class object , which has been deifned in QML from ContextProperty and want to use the id variable as an index values to retrieve QImage from a List. Here is the main function code:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ImageProvider *imageProvider = new ImageProvider;
QQmlApplicationEngine engine;
PageBuffer p;
engine.rootContext()->setContextProperty("p",&p);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.addImageProvider("images", imageProvider);
return app.exec();
}
PageBuffer p contains a List of QImages that i need to present using QQuick ImageProvider when a certain index value for the List has been selected by the user. Here is the snippet of QML code where i want to pass the index value to the imageprovider, it shows an Image Element that will display one of the elements from the List of the Qimages that is saved in the PageBuffer object:
Image{
x: 4
y: 4
height : imagerec.height
visible: true
width : imagerec.width
anchors.fill: imagerec
source:fileUrl
Text{
id:txt
x: 0
y: 71
text:"Sketch"+(index+1)
horizontalAlignment: txt.AlignHCenter
font.family: "Tahoma"
color:"#ffffff"
}
MouseArea {
anchors.rightMargin: -59
anchors.bottomMargin: -39
anchors.fill: parent
onClicked: {
p.index=index;
p.image=mod.get(index).fileUrl
images.image=p.img
// main.source="image://image/1"
// main.source=p.image
// console.log(mod.get(index).fileUrl)
// main.source=p.image;
// currentimage=m.image;
}
}
}
回答1:
You can set objectName
of p
in c++ code.
Assume this name is MyObject
. So now you can pass this name (as a string) to your image provider(imageprovider
in this case):
main.source="image://imageprovider/MyObject"
As I understand you already have your own class, derived from QQuickImageProvider
and overided requestImage
. In your case id
will be "MyObject"
. So now you easy can get real class by its objectName
:
QQuickWindow *window = qobject_cast<QQuickWindow*>(engine.rootObjects()[0]);
PageBuffer *p = window->findChild<PageBuffer *>(id);
I don't sure with accessing the root item, check it by yourself please.
UPDATED:
You can declare ImageProvider
as:
imageprovider.h
class ImageProvider : public QQuickImageProvider
{
public:
ImageProvider (QQmlEngine *engine,ImageType type, Flags flags = 0);
private:
QQmlEngine *m_engine;
}
imageprovider.cpp
ImageProvider ::ImageProvider (QQmlEngine *engine, ImageType type, Flags flags) :
QQuickImageProvider(type,flags),
m_engine(engine)
{
}
QImage ImageProvider ::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
QQuickWindow *window = qobject_cast<QQuickWindow*>(m_engine.rootObjects()[0]);
PageBuffer *p = window->findChild<PageBuffer *>(id);
// do with p what you want
}
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
ImageProvider *imageProvider = new ImageProvider(&engine,QQmlImageProviderBase::Image,0);
PageBuffer p;
engine.rootContext()->setContextProperty("p",&p);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.addImageProvider("images", imageProvider);
return app.exec();
}
来源:https://stackoverflow.com/questions/29929643/using-qimage-with-qquickimageprovider