other dimension is set in proportion to preserve the source image's aspect ratio.
(The \l fillMode is independent of this.)
+ If both the sourceSize.width and sourceSize.height are set the image will be scaled
+ down to fit within the specified size, maintaining the image's aspect ratio. The actual
+ size of the image after scaling is available via \l implicitWidth and \l implicitHeight.
+
If the source is an intrinsically scalable image (eg. SVG), this property
determines the size of the loaded image regardless of intrinsic size.
Avoid changing this property dynamically; rendering an SVG is \e slow compared
be no greater than this property specifies. For some formats (currently only JPEG),
the whole image will never actually be loaded into memory.
- Since QtQuick 1.1 the sourceSize can be cleared to the natural size of the image
+ sourceSize can be cleared to the natural size of the image
by setting sourceSize to \c undefined.
\note \e {Changing this property dynamically causes the image source to be reloaded,
force_scale = true;
}
- bool scaled = false;
if (requestSize.width() > 0 || requestSize.height() > 0) {
QSize s = imgio.size();
+ qreal ratio = 0.0;
if (requestSize.width() && (force_scale || requestSize.width() < s.width())) {
- if (requestSize.height() <= 0)
- s.setHeight(s.height()*requestSize.width()/s.width());
- s.setWidth(requestSize.width()); scaled = true;
+ ratio = qreal(requestSize.width())/s.width();
}
if (requestSize.height() && (force_scale || requestSize.height() < s.height())) {
- if (requestSize.width() <= 0)
- s.setWidth(s.width()*requestSize.height()/s.height());
- s.setHeight(requestSize.height()); scaled = true;
+ qreal hr = qreal(requestSize.height())/s.height();
+ if (ratio == 0.0 || hr < ratio)
+ ratio = hr;
+ }
+ if (ratio > 0.0) {
+ s.setHeight(qRound(s.height() * ratio));
+ s.setWidth(qRound(s.width() * ratio));
+ imgio.setScaledSize(s);
}
- if (scaled) { imgio.setScaledSize(s); }
}
if (impsize)
void sourceSize_QTBUG_16389();
void nullPixmapPaint();
void imageCrash_QTBUG_22125();
+ void sourceSize_data();
+ void sourceSize();
private:
QDeclarativeEngine engine;
QVERIFY(img.pixel(x, y) == qRgb(0, 255, 0));
}
}
+
+ delete canvas;
}
void tst_qquickimage::tiling_QTBUG_6716_data()
qInstallMsgHandler(previousMsgHandler);
QVERIFY(numberOfWarnings == 0);
delete image;
+
+ delete canvas;
}
void tst_qquickimage::imageCrash_QTBUG_22125()
QCoreApplication::processEvents();
}
+void tst_qquickimage::sourceSize_data()
+{
+ QTest::addColumn<int>("sourceWidth");
+ QTest::addColumn<int>("sourceHeight");
+ QTest::addColumn<qreal>("implicitWidth");
+ QTest::addColumn<qreal>("implicitHeight");
+
+ QTest::newRow("unscaled") << 0 << 0 << 300.0 << 300.0;
+ QTest::newRow("scale width") << 100 << 0 << 100.0 << 100.0;
+ QTest::newRow("scale height") << 0 << 150 << 150.0 << 150.0;
+ QTest::newRow("larger sourceSize") << 400 << 400 << 300.0 << 300.0;
+}
+
+void tst_qquickimage::sourceSize()
+{
+ QFETCH(int, sourceWidth);
+ QFETCH(int, sourceHeight);
+ QFETCH(qreal, implicitWidth);
+ QFETCH(qreal, implicitHeight);
+
+ QQuickView *canvas = new QQuickView(0);
+ QDeclarativeContext *ctxt = canvas->rootContext();
+ ctxt->setContextProperty("srcWidth", sourceWidth);
+ ctxt->setContextProperty("srcHeight", sourceHeight);
+
+ canvas->setSource(testFileUrl("sourceSize.qml"));
+ canvas->show();
+ qApp->processEvents();
+
+ QQuickImage *image = qobject_cast<QQuickImage*>(canvas->rootObject());
+ QVERIFY(image);
+
+ QCOMPARE(image->sourceSize().width(), sourceWidth);
+ QCOMPARE(image->sourceSize().height(), sourceHeight);
+ QCOMPARE(image->implicitWidth(), implicitWidth);
+ QCOMPARE(image->implicitHeight(), implicitHeight);
+
+ delete canvas;
+}
+
QTEST_MAIN(tst_qquickimage)
#include "tst_qquickimage.moc"