将QImage与QQuickImageProvider一起使用 [英] Using QImage With QQuickImageProvider

查看:613
本文介绍了将QImage与QQuickImageProvider一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个继承QQuickImageProvider类的类, 但是我想使用QQuickImageProvider的requestImage()函数来设置QImage变量,但是我不知道该怎么做,因为我需要来自ContextProperty的QML中定义的类对象的QImage变量,并希望使用id变量作为索引值以从列表中检索QImage.这是主要的功能代码:

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包含一个QImage列表,当用户为该列表选择了某个索引值时,我需要使用QQuick ImageProvider呈现该列表. 这是QML代码的片段,我想将索引值传递给imageprovider,它显示了一个Image元素,该元素将显示保存在PageBuffer对象中的Qimages列表中的元素之一:

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;
                        }
                    }
                }

推荐答案

您可以在c ++代码中设置pobjectName. 假设此名称为MyObject.因此,现在您可以将此名称(作为字符串)传递给图像提供程序(在本例中为imageprovider):

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"

据我了解,您已经拥有自己的类,该类派生自QQuickImageProvider并覆盖了requestImage.在您的情况下,id将为"MyObject".因此,现在您可以轻松地通过objectName获得真实的课程:

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.

已更新:

您可以将ImageProvider声明为:

imageprovider.h

imageprovider.h

class ImageProvider : public QQuickImageProvider
{
public:
    ImageProvider (QQmlEngine *engine,ImageType type, Flags flags = 0);
private:
    QQmlEngine *m_engine;
}

imageprovider.cpp

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();
}

这篇关于将QImage与QQuickImageProvider一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆