QQuickImageProvider::requestImage() ignore size param values



  • Hi all

    I'm developing an app made in QML and using QQuickImageProvider::requestImage() provider for load custom images. Based to the documentation it seem that the *size param have to be filled with the image width and height to use in case they are not set into QML Image {} item. However, in my experiment, it seem QML engine totally ignore the *size values I set and use as params the width and height got from the QImage object returned.
    Is this behaviour normal and I missed some point in t he use of this function or there is some other problem?
    Thank you


  • Moderators

    @Suppaman
    the docs say:

    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.

    QQuickImageProvider never is responsible in which size the image should be displayed.



  • never is responsible is ok, but from my understanding the param *size is needed for know the size of the image in case an explicit width and heigh was not set inside Image {}. However it doesn't seem the case...


  • Moderators

    @Suppaman
    May it be that you mix up the Image element's sourceSize and the actual item size?



  • No, QML code is really basic as follow:

    Image {
                anchors.centerIn: parent
                source: "image://MyProvider/MyImage"
                cache: false
            }
    

    And the C++ side:

    QImage MyImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
    {
        QImage Image;
    
         // Load image of 70x70 (example) from somewhere
         // .....
    
       *size = QSize(400, 400);//Set as try, this value is totally ignored
    
        return Image;
    }
    

    The image showed is always 70x70


  • Moderators

    @Suppaman said in QQuickImageProvider::requestImage() ignore size param values:

    The image showed is always 70x70

    Because there is no more image data than 70x70 available.
    Again...the QQuickImageProvider doesn't decide how big an image is displayed. You should always set size to the actual size of the loaded image.

    If you want to control the scaling do this instead:

    QImage MyImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
    {
        QImage Image;
    
         // Load image of 70x70 (example) from somewhere
         // .....
        
       Image = Image.scaled(400,400,Qt::KeepAspectRatio,Qt::SmoothTransformation);
    
       *size = Image.size();
    
        return Image;
    }
    


  • Sorry, probably I explained wrong my problem. I know QQuickImageProvider just get the image and pass to the QML layer but in my example code I expected on QML side the image will be scaled to 400x400 since I passed to the size param this values and, based to the documentation, I should "inform" QML Image {} item that the image to show have 400x400 size. Basically same result of this:

    Image {
                anchors.centerIn: parent
                source: "image://MyProvider/MyImage"
                cache: false 
               width: 400
              height: 400
            }
    

    However this not happen and the image showed is always 70x70 (also the size of Image item is 70x70 as obvious). It seem the Image {} get the image size from Image.size() instead of *size param...


  • Moderators

    @Suppaman
    either scale the image in the imageprovider (as i've shown) or adjust the sourceSize of the Image element.
    Again nevertheless always set the size parameter of the actual size of the returned image.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.