QQuickImageProvider "requestedSize" always invalid

时间:2016-01-17 18:55:00

标签: qt qml qt5 qtquick2

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?

1 个答案:

答案 0 :(得分:0)

这是您传递给Image QML元素的sourceSize属性的内容。

如何处理价值:if (!requested_size.isValid() || requested_size.isNull())然后给出原始大小,否则适合requested_sizerequested_size的一个组成部分为零意味着相应的维度是无界的。

如何处理该值(假设native_size(texture_handle)返回图像的默认大小):

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;
}

此代码与标准实现略有不同,因为它允许返回大于其原始大小的图像。对纹理有好处,但在你的情况下可能是也可能不是。