QQuickImageProvider::requestImage 图片缩放,如何处理

QQuickImageProvider::requestImage image scaling, how to handle it

我正在尝试实现一个简单的 PxImageProvider。源图像是静态的,我只提供一张图像。 似乎 requestedSize 总是空的。即使当我尝试在 QML 端修改图像大小时,图像得到 re-scaled,但看起来我的 PxImageProvider 没有进行重新缩放...这正常吗?

我所做的是实现我的 PxImageProvider 子类,以便当 requestedSize 为空(宽度或高度为空或负数)时,我提供翻转后的原始图像(所以我知道我没有进行任何重新缩放)。 但即使 QML 端试图重新缩放图像,我总是看到我的图像翻转(rescaled,but flipped)。

我的.h header:

#include <QQmlApplicationEngine>
#include <QQuickImageProvider>
#include <QImage>

class MyImageProvider :
    public QQuickImageProvider
{
public:
  MyImageProvider(QQmlApplicationEngine *engine, const QString &qmlId, const QImage *image = Q_NULLPTR);
  QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override;
  void setImage(const QImage &image);

private:
  QImage _image;
};

和我的.cpp:

#include "myimageprovider.h"

MyImageProvider::MyImageProvider(QQmlApplicationEngine *engine, const QString & qmlId, const QImage *image) :
  QQuickImageProvider(QQuickImageProvider::Image)
{
  if (image == Q_NULLPTR) {
    _image = QImage();
  }
  else {
    _image = *image;
  }
  engine->addImageProvider(qmlId, this);
}

QImage MyImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
  // Ignoring the id field, just one image to output
  *size = _image.size();
  if (requestedSize.isEmpty())
    return (_image.mirrored(true, true));
  else
    return (_image.scaled(requestedSize, Qt::KeepAspectRatio));
}

void MyImageProvider::setImage(const QImage &image)
{
  _image = image;
}

我创建了一个实例,为它提供了一个 100x100 像素的图像。 在 QML 方面:

    Rectangle {
      id: myImageBlock
      color: "grey"
      width: 250
      height: 250
      anchors.centerIn: parent

      Image {
        id: myImage
        source: "image://my_image/unusedId"
        anchors.centerIn: parent
        width: 50
        height: 50
      }
    }

我确实得到了我的图像,在 250x250 的灰色正方形上正确缩放到 50x50...但是图像被翻转了,这意味着我的提供商没有缩放图像。 这是它应该如何工作吗?

docs 中所述:

QImage QQuickImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)

Implement this method to return the image with id. The default implementation returns an empty image.

The id is the requested image source, with the "image:" scheme and provider identifier removed. For example, if the image source was "image://myprovider/icons/home", the given id would be "icons/home".

The requestedSize corresponds to the Image::sourceSize requested by an Image item. If requestedSize is a valid size, the image returned should be of that size.

In all cases, size must be set to the original size of the image. This is used to set the width and height of the relevant Image if these values have not been set explicitly.

Note: this method may be called by multiple threads, so ensure the implementation of this method is reentrant.

(强调我的)

您必须使用 sourceSize of Image

Image {
    id: myImage
    source: "image://my_image/unusedId"
    anchors.centerIn: parent
    width: 50
    height: 50
    sourceSize.width: 50  // <---
    sourceSize.height: 50 // <---
}