什么是正确的方式使用QQuickImageProvider? [英] What is the right way to use QQuickImageProvider?

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

问题描述

我需要动态选择qpixmaps来显示在QML Image项目内部。 qpixmaps应该从源qpixmap裁剪,我将从QML文件设置。我希望它们被C ++代码裁剪的第一个需求从QML和被缓存以备将来使用。对于动态图像操作它应该从我的自己的类从QQuickImageProvider和加载到QML应用程序引擎。但是如何控制源qpixmap呢?通过财产?如果是,那么我的自定义提供程序必须从QObject派生,它的实例应该在QML中声明,不是吗?但是如何才能被引擎加载呢?我觉得这种实现方式是错误的,但是哪种方式是正确的?

I need to select dynamically qpixmaps to display inside QML Image items. That qpixmaps should be cropped from the source qpixmap, which I'm going to set from the QML file. I'd like them to be cropped by C++ code on the first demand from QML and to be cached for the future use. For dynamical images manipulating it's ought to derive my own class from QQuickImageProvider and load it to QML Application engine. But how can I control the source qpixmap then? Via property? If yes, then my custom provider must be derived from QObject and it's instance should be declared inside QML, isn't it? But how can it be loaded by the engine then? I feel that this way of implementing is wrong, but which one would be correct?

UPD:好吧,我有一个类:

UPD: Ok, I have a class:

class MyQuickImageProvider : public QQuickImageProvider {
public:
    ...
    // This method should set the source image path
    void setPath ( QUrl path );
    // Overriden method of base class; should return cropped image
    virtual QPixmap requestPixmap ( const QString &id, QSize *size, const QSize &requestedSize );
    ...
}

在main.cpp中加载为

In main.cpp it's loaded as:

QQmlApplicationEngine engine;
...
engine.addImageProvider("my_quick_image_provider", new MyQuickImageProvider(QQmlImageProviderBase::Image));

我想通过QML更改源图像路径。如何让setPath方法访问它?明显的方法是将方法声明为Q_INVOKABLE(并从QObject和qmlRegisterType派生MyQuickImageProvider),但是我应该在QML源中声明我的类的实例:

I'd like to change source image path via QML. How can I make setPath method accessible to it? The obvious way is to declare the method as Q_INVOKABLE (and derive MyQuickImageProvider from QObject and qmlRegisterType it), but then I should declare the instance of my class within the QML source:

MyQuickImageProvider {
    id: my_quick_image_provider
    ...
}

从main.cpp访问它会有问题。这样的设计对我来说似乎很奇怪。

The access to it from main.cpp will be problematic. And such a design seems weird to me. Is there any more elegant solution?

推荐答案

您不使用 MyQuickImageProvider 作为QML对象,也不定义 Q_INVOKABLE 方法,因为您无法从QML访问图像提供程序对象。

You do not use MyQuickImageProvider as an QML object, nor do you define a Q_INVOKABLE method because you cannot access the image provider object from QML.

engine.addImageProvider(my_quick_image_provider,[...]

设置访问图像的名称,例如

sets the name how you access images, e.g.

// qml file
Image {
    source: "image://my_quick_image_provider/name_of_my_image"
}

后面的部分name_of_my_image被称为 id ,您在

The later part "name_of_my_image" is called the id, which you find in

virtual QPixmap requestPixmap ( const QString &id, QSize *size, const QSize &requestedSize );

现在实现 requestPixmap MyQuickImageProvider 中,并使用 id 字符串来生成QPixmap。

Now implement requestPixmap in your MyQuickImageProvider and let it use the id string to produce a QPixmap.

我认为你可以抛弃 void setPath(QUrl path); 方法,因为你只需要一个图像提供者实例。

I think you can throw away the void setPath ( QUrl path ); method because you only need one image provider instance for all images of that kind.

由于构造函数不是继承自基类,我不认为 new MyQuickImageProvider(QQmlImageProviderBase :: Image)); 是有意义的。更好地添加没有参数的构造函数

Since constructors are not inherited from the base class, I don't think new MyQuickImageProvider(QQmlImageProviderBase::Image)); makes sense. Better add a constructor with no argument

class MyQuickImageProvider : public QQuickImageProvider {
public:
    MyQuickImageProvider();
    // ...

并且在初始化器列表中具有图像类型:

and have the image type in your initializer list:

MyQuickImageProvider::MyQuickImageProvider()
    : QQuickImageProvider(QQuickImageProvider::Pixmap)
{
}

这篇关于什么是正确的方式使用QQuickImageProvider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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