ManuelSchneid3r
ManuelSchneid3r

Reputation: 16091

QQuickImageProvider "requestedSize" always invalid

I requested my personal imageprovider, but when I debug this few lines requestedSize is always {-1,-1}

class XdgIconThemeImageProvider : public QQuickImageProvider
{
public:
    XdgIconThemeImageProvider() : QQuickImageProvider(QQuickImageProvider::Pixmap){}
    QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
    {
        QIcon ico = QIcon::fromTheme(id);
        QPixmap pm =  ico.isNull() ? QPixmap() : ico.pixmap(100,100);
        *size = pm.size();
        return pm;
    }
};

qmlfile

Image {
    id: icon
    source: model.decoration
    width: parent.height
    height: width
    anchors.centerIn: parent
}

Am I doing something wrong?

Upvotes: 1

Views: 269

Answers (1)

Velkan
Velkan

Reputation: 7582

It's what you're passing to the sourceSize property of the Image QML element.

How to deal with the the value: if (!requested_size.isValid() || requested_size.isNull()) then give native size, otherwise fit in the requested_size. Zero in one of the components of the requested_size means that the respective dimension is unbounded.

How to process the value (supposing that native_size(texture_handle) returns the default size of your image):

const QSize size = native_size(texture_handle);

if (requested_size.isValid()) {
    const QSize bounding_size(requested_size.width() > 0 ? requested_size.width() : size.width(), requested_size.height() > 0 ? requested_size.height() : size.height());

    // If requested_size.isEmpty() then the caller don't care how big one or two dimensions can grow.
    return size.scaled(bounding_size, requested_size.isEmpty() && (requested_size.width() > size.width() || requested_size.height() > size.height()) ? Qt::KeepAspectRatioByExpanding : Qt::KeepAspectRatio);
} else {
    return size;
}

This code differs a bit from the standard implementation because it allows returning images bigger than their native size. Good for textures, but may or may not be desirable in your case.

Upvotes: 1

Related Questions