gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:02:21 25 4
gpt4 key购买 nike

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

我所做的是实现我的 PxImageProvider 子类,以便当 requestedSize 为空(宽度或高度为 null 或负数)时,我提供翻转后的原始图像(所以我知道我没有进行任何重新缩放)。但即使 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.

(强调我的)

您必须使用 sourceSizeImage .

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

关于c++ - QQuickImageProvider::requestImage 图片缩放,如何处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57376246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com