Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QQuickImageProvider::requestImage图像缩放,如何处理_C++_Qt_Qml_Qt5_Qimage - Fatal编程技术网

C++ QQuickImageProvider::requestImage图像缩放,如何处理

C++ QQuickImageProvider::requestImage图像缩放,如何处理,c++,qt,qml,qt5,qimage,C++,Qt,Qml,Qt5,Qimage,我正在尝试实现一个简单的PxImageProvider。源图像是静态的,我只提供一个图像。 似乎requestedSize总是空的。即使当我试图修改QML端的图像大小时,图像也会被重新缩放,但我的PxImageProvider看起来并没有进行重新缩放。。。这正常吗 我所做的是实现我的PxImageProvider子类,这样当requestedSize为空(宽度或高度为null或负值)时,我提供翻转的原始图像(因此我知道我没有进行任何重新缩放)。 但即使QML端尝试重新缩放图像,我也总是看到图像翻

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

我所做的是实现我的PxImageProvider子类,这样当requestedSize为空(宽度或高度为null或负值)时,我提供翻转的原始图像(因此我知道我没有进行任何重新缩放)。 但即使QML端尝试重新缩放图像,我也总是看到图像翻转(重新缩放,但翻转)

我的.h标题:

#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;
};
我创建了一个实例,为它提供一个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。。。但是图像被翻转,这意味着我的提供商没有缩放图像。 这就是它的工作原理吗?

如中所述:

QImage QQuickImageProvider::requestImage(常量QString&id,QSize *大小、常量大小和请求大小)

实现此方法以返回id为的图像。默认实现返回空图像

id是请求的图像源,带有“image:”方案和 已删除提供程序标识符。例如,如果图像源是 "image://myprovider/icons/home,则给定的id将是“icons/home”

requestedSize对应于 图像项。如果requestedSize是有效大小,则返回图像 应该是那种尺寸。

在所有情况下,大小必须设置为图像的原始大小。这 用于设置相关图像的宽度和高度,如果 尚未明确设置值

注意:此方法可能由多个线程调用,因此请确保 该方法的实现是可重入的

(强调矿山)

你必须使用的

图像{
id:myImage
来源:“image://my_image/unusedId"
anchors.centerIn:父对象
宽度:50
身高:50

sourceSize.width:50//我完全错过了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
      }
    }
Image {
    id: myImage
    source: "image://my_image/unusedId"
    anchors.centerIn: parent
    width: 50
    height: 50
    sourceSize.width: 50  // <---
    sourceSize.height: 50 // <---
}