From 4cd5bcf2aa093fdc3861cd4e91aa7850f38de158 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Fri, 30 Apr 2010 14:03:39 +1000 Subject: Initialize variable. --- src/declarative/util/qdeclarativeconnections.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/declarative/util/qdeclarativeconnections.cpp b/src/declarative/util/qdeclarativeconnections.cpp index c23b623..5dd825e 100644 --- a/src/declarative/util/qdeclarativeconnections.cpp +++ b/src/declarative/util/qdeclarativeconnections.cpp @@ -57,7 +57,7 @@ QT_BEGIN_NAMESPACE class QDeclarativeConnectionsPrivate : public QObjectPrivate { public: - QDeclarativeConnectionsPrivate() : target(0), ignoreUnknownSignals(false), componentcomplete(false) {} + QDeclarativeConnectionsPrivate() : target(0), targetSet(false), ignoreUnknownSignals(false), componentcomplete(false) {} QList boundsignals; QObject *target; -- cgit v0.12 From 94853c6310bb0f9f3ca08e3c1fa0976ec3d8d285 Mon Sep 17 00:00:00 2001 From: Leonardo Sobral Cunha Date: Fri, 30 Apr 2010 13:36:29 +1000 Subject: Avoid divisions by zero in qdeclarativetimeline Reviewed-by: Martin Jones --- src/declarative/util/qdeclarativetimeline.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/declarative/util/qdeclarativetimeline.cpp b/src/declarative/util/qdeclarativetimeline.cpp index 656c62b..291b2f3 100644 --- a/src/declarative/util/qdeclarativetimeline.cpp +++ b/src/declarative/util/qdeclarativetimeline.cpp @@ -387,7 +387,10 @@ void QDeclarativeTimeLine::set(QDeclarativeTimeLineValue &timeLineValue, qreal v */ int QDeclarativeTimeLine::accel(QDeclarativeTimeLineValue &timeLineValue, qreal velocity, qreal acceleration) { - if ((velocity > 0.0f) == (acceleration > 0.0f)) + if (acceleration == 0.0f) + return -1; + + if ((velocity > 0.0f) == (acceleration > 0.0f)) acceleration = acceleration * -1.0f; int time = static_cast(-1000 * velocity / acceleration); @@ -410,13 +413,16 @@ int QDeclarativeTimeLine::accel(QDeclarativeTimeLineValue &timeLineValue, qreal */ int QDeclarativeTimeLine::accel(QDeclarativeTimeLineValue &timeLineValue, qreal velocity, qreal acceleration, qreal maxDistance) { - Q_ASSERT(acceleration >= 0.0f && maxDistance >= 0.0f); + if (maxDistance == 0.0f || acceleration == 0.0f) + return -1; + + Q_ASSERT(acceleration > 0.0f && maxDistance > 0.0f); qreal maxAccel = (velocity * velocity) / (2.0f * maxDistance); if (maxAccel > acceleration) acceleration = maxAccel; - if ((velocity > 0.0f) == (acceleration > 0.0f)) + if ((velocity > 0.0f) == (acceleration > 0.0f)) acceleration = acceleration * -1.0f; int time = static_cast(-1000 * velocity / acceleration); @@ -438,6 +444,7 @@ int QDeclarativeTimeLine::accelDistance(QDeclarativeTimeLineValue &timeLineValue { if (distance == 0.0f || velocity == 0.0f) return -1; + Q_ASSERT((distance >= 0.0f) == (velocity >= 0.0f)); int time = static_cast(1000 * (2.0f * distance) / velocity); -- cgit v0.12 From 14b067f85f92194f2c444259e2a70fb095dfb73f Mon Sep 17 00:00:00 2001 From: Leonardo Sobral Cunha Date: Fri, 30 Apr 2010 13:53:20 +1000 Subject: Fix assert in qdeclarativepathview Task-number: QTBUG-10327 Reviewed-by: Martin Jones --- src/declarative/graphicsitems/qdeclarativepathview.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/declarative/graphicsitems/qdeclarativepathview.cpp b/src/declarative/graphicsitems/qdeclarativepathview.cpp index 911f5a4..2f963cd 100644 --- a/src/declarative/graphicsitems/qdeclarativepathview.cpp +++ b/src/declarative/graphicsitems/qdeclarativepathview.cpp @@ -966,7 +966,12 @@ void QDeclarativePathView::mouseReleaseEvent(QGraphicsSceneMouseEvent *) else dist = qRound(dist - d->offset) + d->offset; // Calculate accel required to stop on item boundary - accel = v2 / (2.0f * qAbs(dist)); + if (dist <= 0.) { + dist = 0.; + accel = 0.; + } else { + accel = v2 / (2.0f * qAbs(dist)); + } } d->moveOffset.setValue(d->offset); d->tl.accel(d->moveOffset, velocity, accel, dist); -- cgit v0.12 From b57ad033b5fa48efd2e57f17d26f541f1f50dc02 Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Fri, 30 Apr 2010 12:47:48 +1000 Subject: Add QML value types for math3d types QVector3D was already supported. Add QVector2D, QVector4D, QQuaternion, and QMatrix4x4. Reviewed-by: Warwick Allison --- src/declarative/qml/qdeclarativevaluetype.cpp | 220 +++++++++++++++++++++ src/declarative/qml/qdeclarativevaluetype_p.h | 147 ++++++++++++++ .../qdeclarativevaluetypes/data/matrix4x4_read.qml | 22 +++ .../data/matrix4x4_write.qml | 21 ++ .../data/quaternion_read.qml | 10 + .../data/quaternion_write.qml | 9 + .../qdeclarativevaluetypes/data/vector2d_read.qml | 8 + .../qdeclarativevaluetypes/data/vector2d_write.qml | 7 + .../qdeclarativevaluetypes/data/vector4d_read.qml | 10 + .../qdeclarativevaluetypes/data/vector4d_write.qml | 9 + .../declarative/qdeclarativevaluetypes/testtypes.h | 30 ++- .../tst_qdeclarativevaluetypes.cpp | 134 +++++++++++++ 12 files changed, 626 insertions(+), 1 deletion(-) create mode 100644 tests/auto/declarative/qdeclarativevaluetypes/data/matrix4x4_read.qml create mode 100644 tests/auto/declarative/qdeclarativevaluetypes/data/matrix4x4_write.qml create mode 100644 tests/auto/declarative/qdeclarativevaluetypes/data/quaternion_read.qml create mode 100644 tests/auto/declarative/qdeclarativevaluetypes/data/quaternion_write.qml create mode 100644 tests/auto/declarative/qdeclarativevaluetypes/data/vector2d_read.qml create mode 100644 tests/auto/declarative/qdeclarativevaluetypes/data/vector2d_write.qml create mode 100644 tests/auto/declarative/qdeclarativevaluetypes/data/vector4d_read.qml create mode 100644 tests/auto/declarative/qdeclarativevaluetypes/data/vector4d_write.qml diff --git a/src/declarative/qml/qdeclarativevaluetype.cpp b/src/declarative/qml/qdeclarativevaluetype.cpp index c6fe161..dbc25bb 100644 --- a/src/declarative/qml/qdeclarativevaluetype.cpp +++ b/src/declarative/qml/qdeclarativevaluetype.cpp @@ -116,8 +116,16 @@ QDeclarativeValueType *QDeclarativeValueTypeFactory::valueType(int t) return new QDeclarativeRectValueType; case QVariant::RectF: return new QDeclarativeRectFValueType; + case QVariant::Vector2D: + return new QDeclarativeVector2DValueType; case QVariant::Vector3D: return new QDeclarativeVector3DValueType; + case QVariant::Vector4D: + return new QDeclarativeVector4DValueType; + case QVariant::Quaternion: + return new QDeclarativeQuaternionValueType; + case QVariant::Matrix4x4: + return new QDeclarativeMatrix4x4ValueType; case QVariant::EasingCurve: return new QDeclarativeEasingValueType; case QVariant::Font: @@ -460,6 +468,54 @@ void QDeclarativeRectValueType::setHeight(int h) rect.setHeight(h); } +QDeclarativeVector2DValueType::QDeclarativeVector2DValueType(QObject *parent) +: QDeclarativeValueType(parent) +{ +} + +void QDeclarativeVector2DValueType::read(QObject *obj, int idx) +{ + void *a[] = { &vector, 0 }; + QMetaObject::metacall(obj, QMetaObject::ReadProperty, idx, a); +} + +void QDeclarativeVector2DValueType::write(QObject *obj, int idx, QDeclarativePropertyPrivate::WriteFlags flags) +{ + int status = -1; + void *a[] = { &vector, 0, &status, &flags }; + QMetaObject::metacall(obj, QMetaObject::WriteProperty, idx, a); +} + +QVariant QDeclarativeVector2DValueType::value() +{ + return QVariant(vector); +} + +void QDeclarativeVector2DValueType::setValue(QVariant value) +{ + vector = qvariant_cast(value); +} + +qreal QDeclarativeVector2DValueType::x() const +{ + return vector.x(); +} + +qreal QDeclarativeVector2DValueType::y() const +{ + return vector.y(); +} + +void QDeclarativeVector2DValueType::setX(qreal x) +{ + vector.setX(x); +} + +void QDeclarativeVector2DValueType::setY(qreal y) +{ + vector.setY(y); +} + QDeclarativeVector3DValueType::QDeclarativeVector3DValueType(QObject *parent) : QDeclarativeValueType(parent) { @@ -518,6 +574,170 @@ void QDeclarativeVector3DValueType::setZ(qreal z) vector.setZ(z); } +QDeclarativeVector4DValueType::QDeclarativeVector4DValueType(QObject *parent) +: QDeclarativeValueType(parent) +{ +} + +void QDeclarativeVector4DValueType::read(QObject *obj, int idx) +{ + void *a[] = { &vector, 0 }; + QMetaObject::metacall(obj, QMetaObject::ReadProperty, idx, a); +} + +void QDeclarativeVector4DValueType::write(QObject *obj, int idx, QDeclarativePropertyPrivate::WriteFlags flags) +{ + int status = -1; + void *a[] = { &vector, 0, &status, &flags }; + QMetaObject::metacall(obj, QMetaObject::WriteProperty, idx, a); +} + +QVariant QDeclarativeVector4DValueType::value() +{ + return QVariant(vector); +} + +void QDeclarativeVector4DValueType::setValue(QVariant value) +{ + vector = qvariant_cast(value); +} + +qreal QDeclarativeVector4DValueType::x() const +{ + return vector.x(); +} + +qreal QDeclarativeVector4DValueType::y() const +{ + return vector.y(); +} + +qreal QDeclarativeVector4DValueType::z() const +{ + return vector.z(); +} + +qreal QDeclarativeVector4DValueType::w() const +{ + return vector.w(); +} + +void QDeclarativeVector4DValueType::setX(qreal x) +{ + vector.setX(x); +} + +void QDeclarativeVector4DValueType::setY(qreal y) +{ + vector.setY(y); +} + +void QDeclarativeVector4DValueType::setZ(qreal z) +{ + vector.setZ(z); +} + +void QDeclarativeVector4DValueType::setW(qreal w) +{ + vector.setW(w); +} + +QDeclarativeQuaternionValueType::QDeclarativeQuaternionValueType(QObject *parent) +: QDeclarativeValueType(parent) +{ +} + +void QDeclarativeQuaternionValueType::read(QObject *obj, int idx) +{ + void *a[] = { &quaternion, 0 }; + QMetaObject::metacall(obj, QMetaObject::ReadProperty, idx, a); +} + +void QDeclarativeQuaternionValueType::write(QObject *obj, int idx, QDeclarativePropertyPrivate::WriteFlags flags) +{ + int status = -1; + void *a[] = { &quaternion, 0, &status, &flags }; + QMetaObject::metacall(obj, QMetaObject::WriteProperty, idx, a); +} + +QVariant QDeclarativeQuaternionValueType::value() +{ + return QVariant(quaternion); +} + +void QDeclarativeQuaternionValueType::setValue(QVariant value) +{ + quaternion = qvariant_cast(value); +} + +qreal QDeclarativeQuaternionValueType::scalar() const +{ + return quaternion.scalar(); +} + +qreal QDeclarativeQuaternionValueType::x() const +{ + return quaternion.x(); +} + +qreal QDeclarativeQuaternionValueType::y() const +{ + return quaternion.y(); +} + +qreal QDeclarativeQuaternionValueType::z() const +{ + return quaternion.z(); +} + +void QDeclarativeQuaternionValueType::setScalar(qreal scalar) +{ + quaternion.setScalar(scalar); +} + +void QDeclarativeQuaternionValueType::setX(qreal x) +{ + quaternion.setX(x); +} + +void QDeclarativeQuaternionValueType::setY(qreal y) +{ + quaternion.setY(y); +} + +void QDeclarativeQuaternionValueType::setZ(qreal z) +{ + quaternion.setZ(z); +} + +QDeclarativeMatrix4x4ValueType::QDeclarativeMatrix4x4ValueType(QObject *parent) +: QDeclarativeValueType(parent) +{ +} + +void QDeclarativeMatrix4x4ValueType::read(QObject *obj, int idx) +{ + void *a[] = { &matrix, 0 }; + QMetaObject::metacall(obj, QMetaObject::ReadProperty, idx, a); +} + +void QDeclarativeMatrix4x4ValueType::write(QObject *obj, int idx, QDeclarativePropertyPrivate::WriteFlags flags) +{ + int status = -1; + void *a[] = { &matrix, 0, &status, &flags }; + QMetaObject::metacall(obj, QMetaObject::WriteProperty, idx, a); +} + +QVariant QDeclarativeMatrix4x4ValueType::value() +{ + return QVariant(matrix); +} + +void QDeclarativeMatrix4x4ValueType::setValue(QVariant value) +{ + matrix = qvariant_cast(value); +} + QDeclarativeEasingValueType::QDeclarativeEasingValueType(QObject *parent) : QDeclarativeValueType(parent) { diff --git a/src/declarative/qml/qdeclarativevaluetype_p.h b/src/declarative/qml/qdeclarativevaluetype_p.h index d1833bb..476c73d 100644 --- a/src/declarative/qml/qdeclarativevaluetype_p.h +++ b/src/declarative/qml/qdeclarativevaluetype_p.h @@ -60,7 +60,11 @@ #include #include #include +#include #include +#include +#include +#include #include QT_BEGIN_NAMESPACE @@ -241,6 +245,28 @@ private: QRect rect; }; +class Q_AUTOTEST_EXPORT QDeclarativeVector2DValueType : public QDeclarativeValueType +{ + Q_PROPERTY(qreal x READ x WRITE setX) + Q_PROPERTY(qreal y READ y WRITE setY) + Q_OBJECT +public: + QDeclarativeVector2DValueType(QObject *parent = 0); + + virtual void read(QObject *, int); + virtual void write(QObject *, int, QDeclarativePropertyPrivate::WriteFlags); + virtual QVariant value(); + virtual void setValue(QVariant value); + + qreal x() const; + qreal y() const; + void setX(qreal); + void setY(qreal); + +private: + QVector2D vector; +}; + class Q_AUTOTEST_EXPORT QDeclarativeVector3DValueType : public QDeclarativeValueType { Q_PROPERTY(qreal x READ x WRITE setX) @@ -266,6 +292,127 @@ private: QVector3D vector; }; +class Q_AUTOTEST_EXPORT QDeclarativeVector4DValueType : public QDeclarativeValueType +{ + Q_PROPERTY(qreal x READ x WRITE setX) + Q_PROPERTY(qreal y READ y WRITE setY) + Q_PROPERTY(qreal z READ z WRITE setZ) + Q_PROPERTY(qreal w READ w WRITE setW) + Q_OBJECT +public: + QDeclarativeVector4DValueType(QObject *parent = 0); + + virtual void read(QObject *, int); + virtual void write(QObject *, int, QDeclarativePropertyPrivate::WriteFlags); + virtual QVariant value(); + virtual void setValue(QVariant value); + + qreal x() const; + qreal y() const; + qreal z() const; + qreal w() const; + void setX(qreal); + void setY(qreal); + void setZ(qreal); + void setW(qreal); + +private: + QVector4D vector; +}; + +class Q_AUTOTEST_EXPORT QDeclarativeQuaternionValueType : public QDeclarativeValueType +{ + Q_PROPERTY(qreal scalar READ scalar WRITE setScalar) + Q_PROPERTY(qreal x READ x WRITE setX) + Q_PROPERTY(qreal y READ y WRITE setY) + Q_PROPERTY(qreal z READ z WRITE setZ) + Q_OBJECT +public: + QDeclarativeQuaternionValueType(QObject *parent = 0); + + virtual void read(QObject *, int); + virtual void write(QObject *, int, QDeclarativePropertyPrivate::WriteFlags); + virtual QVariant value(); + virtual void setValue(QVariant value); + + qreal scalar() const; + qreal x() const; + qreal y() const; + qreal z() const; + void setScalar(qreal); + void setX(qreal); + void setY(qreal); + void setZ(qreal); + +private: + QQuaternion quaternion; +}; + +class Q_AUTOTEST_EXPORT QDeclarativeMatrix4x4ValueType : public QDeclarativeValueType +{ + Q_PROPERTY(qreal m11 READ m11 WRITE setM11) + Q_PROPERTY(qreal m12 READ m12 WRITE setM12) + Q_PROPERTY(qreal m13 READ m13 WRITE setM13) + Q_PROPERTY(qreal m14 READ m14 WRITE setM14) + Q_PROPERTY(qreal m21 READ m21 WRITE setM21) + Q_PROPERTY(qreal m22 READ m22 WRITE setM22) + Q_PROPERTY(qreal m23 READ m23 WRITE setM23) + Q_PROPERTY(qreal m24 READ m24 WRITE setM24) + Q_PROPERTY(qreal m31 READ m31 WRITE setM31) + Q_PROPERTY(qreal m32 READ m32 WRITE setM32) + Q_PROPERTY(qreal m33 READ m33 WRITE setM33) + Q_PROPERTY(qreal m34 READ m34 WRITE setM34) + Q_PROPERTY(qreal m41 READ m41 WRITE setM41) + Q_PROPERTY(qreal m42 READ m42 WRITE setM42) + Q_PROPERTY(qreal m43 READ m43 WRITE setM43) + Q_PROPERTY(qreal m44 READ m44 WRITE setM44) + Q_OBJECT +public: + QDeclarativeMatrix4x4ValueType(QObject *parent = 0); + + virtual void read(QObject *, int); + virtual void write(QObject *, int, QDeclarativePropertyPrivate::WriteFlags); + virtual QVariant value(); + virtual void setValue(QVariant value); + + qreal m11() const { return matrix(0, 0); } + qreal m12() const { return matrix(0, 1); } + qreal m13() const { return matrix(0, 2); } + qreal m14() const { return matrix(0, 3); } + qreal m21() const { return matrix(1, 0); } + qreal m22() const { return matrix(1, 1); } + qreal m23() const { return matrix(1, 2); } + qreal m24() const { return matrix(1, 3); } + qreal m31() const { return matrix(2, 0); } + qreal m32() const { return matrix(2, 1); } + qreal m33() const { return matrix(2, 2); } + qreal m34() const { return matrix(2, 3); } + qreal m41() const { return matrix(3, 0); } + qreal m42() const { return matrix(3, 1); } + qreal m43() const { return matrix(3, 2); } + qreal m44() const { return matrix(3, 3); } + + void setM11(qreal value) { matrix(0, 0) = value; } + void setM12(qreal value) { matrix(0, 1) = value; } + void setM13(qreal value) { matrix(0, 2) = value; } + void setM14(qreal value) { matrix(0, 3) = value; } + void setM21(qreal value) { matrix(1, 0) = value; } + void setM22(qreal value) { matrix(1, 1) = value; } + void setM23(qreal value) { matrix(1, 2) = value; } + void setM24(qreal value) { matrix(1, 3) = value; } + void setM31(qreal value) { matrix(2, 0) = value; } + void setM32(qreal value) { matrix(2, 1) = value; } + void setM33(qreal value) { matrix(2, 2) = value; } + void setM34(qreal value) { matrix(2, 3) = value; } + void setM41(qreal value) { matrix(3, 0) = value; } + void setM42(qreal value) { matrix(3, 1) = value; } + void setM43(qreal value) { matrix(3, 2) = value; } + void setM44(qreal value) { matrix(3, 3) = value; } + +private: + QMatrix4x4 matrix; +}; + class Q_AUTOTEST_EXPORT QDeclarativeEasingValueType : public QDeclarativeValueType { Q_OBJECT diff --git a/tests/auto/declarative/qdeclarativevaluetypes/data/matrix4x4_read.qml b/tests/auto/declarative/qdeclarativevaluetypes/data/matrix4x4_read.qml new file mode 100644 index 0000000..6c4a682 --- /dev/null +++ b/tests/auto/declarative/qdeclarativevaluetypes/data/matrix4x4_read.qml @@ -0,0 +1,22 @@ +import Test 1.0 + +MyTypeObject { + property real v_m11: matrix.m11 + property real v_m12: matrix.m12 + property real v_m13: matrix.m13 + property real v_m14: matrix.m14 + property real v_m21: matrix.m21 + property real v_m22: matrix.m22 + property real v_m23: matrix.m23 + property real v_m24: matrix.m24 + property real v_m31: matrix.m31 + property real v_m32: matrix.m32 + property real v_m33: matrix.m33 + property real v_m34: matrix.m34 + property real v_m41: matrix.m41 + property real v_m42: matrix.m42 + property real v_m43: matrix.m43 + property real v_m44: matrix.m44 + property variant copy: matrix +} + diff --git a/tests/auto/declarative/qdeclarativevaluetypes/data/matrix4x4_write.qml b/tests/auto/declarative/qdeclarativevaluetypes/data/matrix4x4_write.qml new file mode 100644 index 0000000..2a9f154 --- /dev/null +++ b/tests/auto/declarative/qdeclarativevaluetypes/data/matrix4x4_write.qml @@ -0,0 +1,21 @@ +import Test 1.0 + +MyTypeObject { + matrix.m11: if (true) 11 + matrix.m12: if (true) 12 + matrix.m13: if (true) 13 + matrix.m14: if (true) 14 + matrix.m21: if (true) 21 + matrix.m22: if (true) 22 + matrix.m23: if (true) 23 + matrix.m24: if (true) 24 + matrix.m31: if (true) 31 + matrix.m32: if (true) 32 + matrix.m33: if (true) 33 + matrix.m34: if (true) 34 + matrix.m41: if (true) 41 + matrix.m42: if (true) 42 + matrix.m43: if (true) 43 + matrix.m44: if (true) 44 +} + diff --git a/tests/auto/declarative/qdeclarativevaluetypes/data/quaternion_read.qml b/tests/auto/declarative/qdeclarativevaluetypes/data/quaternion_read.qml new file mode 100644 index 0000000..d1a21dc --- /dev/null +++ b/tests/auto/declarative/qdeclarativevaluetypes/data/quaternion_read.qml @@ -0,0 +1,10 @@ +import Test 1.0 + +MyTypeObject { + property real v_scalar: quaternion.scalar + property real v_x: quaternion.x + property real v_y: quaternion.y + property real v_z: quaternion.z + property variant copy: quaternion +} + diff --git a/tests/auto/declarative/qdeclarativevaluetypes/data/quaternion_write.qml b/tests/auto/declarative/qdeclarativevaluetypes/data/quaternion_write.qml new file mode 100644 index 0000000..0c3e5af --- /dev/null +++ b/tests/auto/declarative/qdeclarativevaluetypes/data/quaternion_write.qml @@ -0,0 +1,9 @@ +import Test 1.0 + +MyTypeObject { + quaternion.scalar: if (true) 88.5 + quaternion.x: if (true) -0.3 + quaternion.y: if (true) -12.9 + quaternion.z: if (true) 907.4 +} + diff --git a/tests/auto/declarative/qdeclarativevaluetypes/data/vector2d_read.qml b/tests/auto/declarative/qdeclarativevaluetypes/data/vector2d_read.qml new file mode 100644 index 0000000..fc315f7 --- /dev/null +++ b/tests/auto/declarative/qdeclarativevaluetypes/data/vector2d_read.qml @@ -0,0 +1,8 @@ +import Test 1.0 + +MyTypeObject { + property real v_x: vector2.x + property real v_y: vector2.y + property variant copy: vector2 +} + diff --git a/tests/auto/declarative/qdeclarativevaluetypes/data/vector2d_write.qml b/tests/auto/declarative/qdeclarativevaluetypes/data/vector2d_write.qml new file mode 100644 index 0000000..f0e35ff --- /dev/null +++ b/tests/auto/declarative/qdeclarativevaluetypes/data/vector2d_write.qml @@ -0,0 +1,7 @@ +import Test 1.0 + +MyTypeObject { + vector2.x: if (true) -0.3 + vector2.y: if (true) -12.9 +} + diff --git a/tests/auto/declarative/qdeclarativevaluetypes/data/vector4d_read.qml b/tests/auto/declarative/qdeclarativevaluetypes/data/vector4d_read.qml new file mode 100644 index 0000000..f9d5d60 --- /dev/null +++ b/tests/auto/declarative/qdeclarativevaluetypes/data/vector4d_read.qml @@ -0,0 +1,10 @@ +import Test 1.0 + +MyTypeObject { + property real v_x: vector4.x + property real v_y: vector4.y + property real v_z: vector4.z + property real v_w: vector4.w + property variant copy: vector4 +} + diff --git a/tests/auto/declarative/qdeclarativevaluetypes/data/vector4d_write.qml b/tests/auto/declarative/qdeclarativevaluetypes/data/vector4d_write.qml new file mode 100644 index 0000000..5486981 --- /dev/null +++ b/tests/auto/declarative/qdeclarativevaluetypes/data/vector4d_write.qml @@ -0,0 +1,9 @@ +import Test 1.0 + +MyTypeObject { + vector4.x: if (true) -0.3 + vector4.y: if (true) -12.9 + vector4.z: if (true) 907.4 + vector4.w: if (true) 88.5 +} + diff --git a/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h b/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h index 8a9b981..1da9990 100644 --- a/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h +++ b/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h @@ -48,7 +48,11 @@ #include #include #include +#include #include +#include +#include +#include #include #include #include @@ -66,7 +70,11 @@ class MyTypeObject : public QObject Q_PROPERTY(QSize sizereadonly READ size NOTIFY changed) Q_PROPERTY(QRect rect READ rect WRITE setRect NOTIFY changed) Q_PROPERTY(QRectF rectf READ rectf WRITE setRectf NOTIFY changed) + Q_PROPERTY(QVector2D vector2 READ vector2 WRITE setVector2 NOTIFY changed) Q_PROPERTY(QVector3D vector READ vector WRITE setVector NOTIFY changed) + Q_PROPERTY(QVector4D vector4 READ vector4 WRITE setVector4 NOTIFY changed) + Q_PROPERTY(QQuaternion quaternion READ quaternion WRITE setQuaternion NOTIFY changed) + Q_PROPERTY(QMatrix4x4 matrix READ matrix WRITE setMatrix NOTIFY changed) Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY changed) public: @@ -77,7 +85,11 @@ public: m_sizef(0.1, 100923.2), m_rect(2, 3, 109, 102), m_rectf(103.8, 99.2, 88.1, 77.6), - m_vector(23.88, 3.1, 4.3) + m_vector2(32.88, 1.3), + m_vector(23.88, 3.1, 4.3), + m_vector4(54.2, 23.88, 3.1, 4.3), + m_quaternion(4.3, 54.2, 23.88, 3.1), + m_matrix(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) { m_font.setFamily("Arial"); m_font.setBold(true); @@ -116,10 +128,26 @@ public: QRectF rectf() const { return m_rectf; } void setRectf(const QRectF &v) { m_rectf = v; emit changed(); } + QVector2D m_vector2; + QVector2D vector2() const { return m_vector2; } + void setVector2(const QVector2D &v) { m_vector2 = v; emit changed(); } + QVector3D m_vector; QVector3D vector() const { return m_vector; } void setVector(const QVector3D &v) { m_vector = v; emit changed(); } + QVector4D m_vector4; + QVector4D vector4() const { return m_vector4; } + void setVector4(const QVector4D &v) { m_vector4 = v; emit changed(); } + + QQuaternion m_quaternion; + QQuaternion quaternion() const { return m_quaternion; } + void setQuaternion(const QQuaternion &v) { m_quaternion = v; emit changed(); } + + QMatrix4x4 m_matrix; + QMatrix4x4 matrix() const { return m_matrix; } + void setMatrix(const QMatrix4x4 &v) { m_matrix = v; emit changed(); } + QFont m_font; QFont font() const { return m_font; } void setFont(const QFont &v) { m_font = v; emit changed(); } diff --git a/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp b/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp index b733b10..c18fbf5 100644 --- a/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp +++ b/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp @@ -62,7 +62,11 @@ private slots: void sizereadonly(); void rect(); void rectf(); + void vector2d(); void vector3d(); + void vector4d(); + void quaternion(); + void matrix4x4(); void font(); void bindingAssignment(); @@ -293,6 +297,31 @@ void tst_qdeclarativevaluetypes::rectf() } } +void tst_qdeclarativevaluetypes::vector2d() +{ + { + QDeclarativeComponent component(&engine, TEST_FILE("vector2d_read.qml")); + MyTypeObject *object = qobject_cast(component.create()); + QVERIFY(object != 0); + + QCOMPARE((float)object->property("v_x").toDouble(), (float)32.88); + QCOMPARE((float)object->property("v_y").toDouble(), (float)1.3); + QCOMPARE(object->property("copy"), QVariant(QVector2D(32.88, 1.3))); + + delete object; + } + + { + QDeclarativeComponent component(&engine, TEST_FILE("vector2d_write.qml")); + MyTypeObject *object = qobject_cast(component.create()); + QVERIFY(object != 0); + + QCOMPARE(object->vector2(), QVector2D(-0.3, -12.9)); + + delete object; + } +} + void tst_qdeclarativevaluetypes::vector3d() { { @@ -319,6 +348,106 @@ void tst_qdeclarativevaluetypes::vector3d() } } +void tst_qdeclarativevaluetypes::vector4d() +{ + { + QDeclarativeComponent component(&engine, TEST_FILE("vector4d_read.qml")); + MyTypeObject *object = qobject_cast(component.create()); + QVERIFY(object != 0); + + QCOMPARE((float)object->property("v_x").toDouble(), (float)54.2); + QCOMPARE((float)object->property("v_y").toDouble(), (float)23.88); + QCOMPARE((float)object->property("v_z").toDouble(), (float)3.1); + QCOMPARE((float)object->property("v_w").toDouble(), (float)4.3); + QCOMPARE(object->property("copy"), QVariant(QVector4D(54.2, 23.88, 3.1, 4.3))); + + delete object; + } + + { + QDeclarativeComponent component(&engine, TEST_FILE("vector4d_write.qml")); + MyTypeObject *object = qobject_cast(component.create()); + QVERIFY(object != 0); + + QCOMPARE(object->vector4(), QVector4D(-0.3, -12.9, 907.4, 88.5)); + + delete object; + } +} + +void tst_qdeclarativevaluetypes::quaternion() +{ + { + QDeclarativeComponent component(&engine, TEST_FILE("quaternion_read.qml")); + MyTypeObject *object = qobject_cast(component.create()); + QVERIFY(object != 0); + + QCOMPARE((float)object->property("v_scalar").toDouble(), (float)4.3); + QCOMPARE((float)object->property("v_x").toDouble(), (float)54.2); + QCOMPARE((float)object->property("v_y").toDouble(), (float)23.88); + QCOMPARE((float)object->property("v_z").toDouble(), (float)3.1); + QCOMPARE(object->property("copy"), QVariant(QQuaternion(4.3, 54.2, 23.88, 3.1))); + + delete object; + } + + { + QDeclarativeComponent component(&engine, TEST_FILE("quaternion_write.qml")); + MyTypeObject *object = qobject_cast(component.create()); + QVERIFY(object != 0); + + QCOMPARE(object->quaternion(), QQuaternion(88.5, -0.3, -12.9, 907.4)); + + delete object; + } +} + +void tst_qdeclarativevaluetypes::matrix4x4() +{ + { + QDeclarativeComponent component(&engine, TEST_FILE("matrix4x4_read.qml")); + MyTypeObject *object = qobject_cast(component.create()); + QVERIFY(object != 0); + + QCOMPARE((float)object->property("v_m11").toDouble(), (float)1); + QCOMPARE((float)object->property("v_m12").toDouble(), (float)2); + QCOMPARE((float)object->property("v_m13").toDouble(), (float)3); + QCOMPARE((float)object->property("v_m14").toDouble(), (float)4); + QCOMPARE((float)object->property("v_m21").toDouble(), (float)5); + QCOMPARE((float)object->property("v_m22").toDouble(), (float)6); + QCOMPARE((float)object->property("v_m23").toDouble(), (float)7); + QCOMPARE((float)object->property("v_m24").toDouble(), (float)8); + QCOMPARE((float)object->property("v_m31").toDouble(), (float)9); + QCOMPARE((float)object->property("v_m32").toDouble(), (float)10); + QCOMPARE((float)object->property("v_m33").toDouble(), (float)11); + QCOMPARE((float)object->property("v_m34").toDouble(), (float)12); + QCOMPARE((float)object->property("v_m41").toDouble(), (float)13); + QCOMPARE((float)object->property("v_m42").toDouble(), (float)14); + QCOMPARE((float)object->property("v_m43").toDouble(), (float)15); + QCOMPARE((float)object->property("v_m44").toDouble(), (float)16); + QCOMPARE(object->property("copy"), + QVariant(QMatrix4x4(1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10, 11, 12, + 13, 14, 15, 16))); + + delete object; + } + + { + QDeclarativeComponent component(&engine, TEST_FILE("matrix4x4_write.qml")); + MyTypeObject *object = qobject_cast(component.create()); + QVERIFY(object != 0); + + QCOMPARE(object->matrix(), QMatrix4x4(11, 12, 13, 14, + 21, 22, 23, 24, + 31, 32, 33, 34, + 41, 42, 43, 44)); + + delete object; + } +} + void tst_qdeclarativevaluetypes::font() { { @@ -665,7 +794,12 @@ void tst_qdeclarativevaluetypes::cppClasses() CPP_TEST(QDeclarativeSizeFValueType, QSizeF(-100.7, 18.2)); CPP_TEST(QDeclarativeRectValueType, QRect(13, 39, 10928, 88)); CPP_TEST(QDeclarativeRectFValueType, QRectF(88.2, -90.1, 103.2, 118)); + CPP_TEST(QDeclarativeVector2DValueType, QVector2D(19.7, 1002)); CPP_TEST(QDeclarativeVector3DValueType, QVector3D(18.2, 19.7, 1002)); + CPP_TEST(QDeclarativeVector4DValueType, QVector4D(18.2, 19.7, 1002, 54)); + CPP_TEST(QDeclarativeQuaternionValueType, QQuaternion(18.2, 19.7, 1002, 54)); + CPP_TEST(QDeclarativeMatrix4x4ValueType, + QMatrix4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)); CPP_TEST(QDeclarativeFontValueType, QFont("Helvetica")); } -- cgit v0.12 From e900d3b5c026ede908a5b8623f044fff6421fdeb Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Fri, 30 Apr 2010 10:56:57 +1000 Subject: Fix error string --- src/declarative/qml/qdeclarativeobjectscriptclass.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp index 33e47fb..03366f0 100644 --- a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp +++ b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp @@ -444,7 +444,7 @@ QScriptValue QDeclarativeObjectScriptClass::destroy(QScriptContext *context, QSc QDeclarativeData *ddata = QDeclarativeData::get(data->object, false); if (!ddata || ddata->indestructible) - return engine->currentContext()->throwError(QLatin1String("Invalid attempt to destroy() an indestructible object")); + return engine->currentContext()->throwError(QLatin1String("Invalid attempt to destroy() an indestructible object")); QObject *obj = data->object; int delay = 0; -- cgit v0.12 From 77cddec6ea642b073d1d9c017c865e740c0c60bc Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Fri, 30 Apr 2010 15:26:55 +1000 Subject: Doc fixes --- doc/src/declarative/codingconventions.qdoc | 6 +- doc/src/declarative/dynamicobjects.qdoc | 65 +++++++++++-------- doc/src/declarative/globalobject.qdoc | 76 +++++++++++------------ doc/src/snippets/declarative/componentCreation.js | 48 +++++++------- src/declarative/qml/qdeclarativecomponent.cpp | 6 +- 5 files changed, 106 insertions(+), 95 deletions(-) diff --git a/doc/src/declarative/codingconventions.qdoc b/doc/src/declarative/codingconventions.qdoc index 7ca206b..d0f873d 100644 --- a/doc/src/declarative/codingconventions.qdoc +++ b/doc/src/declarative/codingconventions.qdoc @@ -57,7 +57,7 @@ Through our documentation and examples, QML objects are always structured in the \o id \o property declarations \o signal declarations -\o javascript functions +\o JavaScript functions \o object properties \o child objects \o states @@ -102,7 +102,7 @@ we will write this: \snippet doc/src/snippets/declarative/codingconventions/lists.qml 1 -\section1 Javascript code +\section1 JavaScript code If the script is a single expression, we recommend writing it inline: @@ -116,7 +116,7 @@ If the script is more than a couple of lines long or can be used by different ob \snippet doc/src/snippets/declarative/codingconventions/javascript.qml 2 -For long scripts, we will put the functions in their own javascript file and import it like this: +For long scripts, we will put the functions in their own JavaScript file and import it like this: \snippet doc/src/snippets/declarative/codingconventions/javascript-imports.qml 0 diff --git a/doc/src/declarative/dynamicobjects.qdoc b/doc/src/declarative/dynamicobjects.qdoc index a2b65a8..c376266 100644 --- a/doc/src/declarative/dynamicobjects.qdoc +++ b/doc/src/declarative/dynamicobjects.qdoc @@ -43,23 +43,25 @@ \page qdeclarativedynamicobjects.html \title Dynamic Object Management -QML has some support for dynamically loading and managing QML objects from -within Javascript blocks. It is preferable to use the existing QML elements for -dynamic object management wherever possible; these are \l{Loader}, -\l{Repeater}, \l{ListView}, \l{GridView} and \l{PathView}. It is also possible -to dynamically create and manage objects from C++, and this is preferable for -hybrid QML/C++ applications - see \l{Using QML in C++ Applications}. -Dynamically creating and managing objects from -within Javascript blocks is intended for when none of the existing QML elements -fit the needs of your application, and you do not desire for your application -to involve C++ code. +QML provides a number of ways to dynamically create and manage QML objects. +The \l{Loader}, \l{Repeater}, \l{ListView}, \l{GridView} and \l{PathView} elements +all support dynamic object management. Objects can also be created and managed +from C++, and this is the preferred method for hybrid QML/C++ applications +(see \l{Using QML in C++ Applications}). + +QML also supports the dynamic creation of objects from within JavaScript +code. This is useful if the existing QML elements do not fit the needs of your +application, and there are no C++ components involved. + \section1 Creating Objects Dynamically -There are two ways of creating objects dynamically. You can either create -a component which instantiates items, or create an item from a string of QML. -Creating a component is better for the situation where you have a predefined -item which you want to manage dynamic instances of, and creating an item from -a string of QML is intended for when the QML itself is generated at runtime. +There are two ways to create objects dynamically from JavaScript. You can either call +\l {Qt.createComponent(url file)}{Qt.createComponent()} to create +a component which instantiates items, or use \l{Qt.createQmlObject(string qml, object parent, string filepath)}{Qt.createQmlObject()} +to create an item from a string of QML. +Creating a component is better if you have a predefined +item, and you want to create dynamic instances of that item; creating an item from +a string of QML is useful when the item QML itself is generated at runtime. If you have a component specified in a QML file, you can dynamically load it with the \l {Qt.createComponent(url file)}{Qt.createComponent()} function on the \l{QML Global Object}. @@ -69,7 +71,7 @@ a component object which can be used to create and load that QML file. Once you have a component you can use its \c createObject() method to create an instance of the component. -Here is an example. Here is a simple QML component defined in \c Sprite.qml: +Here is an example. Here is a \c Sprite.qml, which defines a simple QML component: \quotefile doc/src/snippets/declarative/Sprite.qml @@ -84,14 +86,10 @@ over the network cannot be expected to be ready immediately: \snippet doc/src/snippets/declarative/componentCreation.js 0 \codeline \snippet doc/src/snippets/declarative/componentCreation.js 1 -\snippet doc/src/snippets/declarative/componentCreation.js 2 -\snippet doc/src/snippets/declarative/componentCreation.js 4 -\codeline -\snippet doc/src/snippets/declarative/componentCreation.js 5 If you are certain the files will be local, you could simplify to: -\snippet doc/src/snippets/declarative/componentCreation.js 3 +\snippet doc/src/snippets/declarative/componentCreation.js 2 Notice that once a \c Sprite object is created, its parent is set to \c appWindow (defined in \c main.qml). After creating an item, you must set its parent to an item within the scene. @@ -114,8 +112,22 @@ item, which is used for error reporting. \section1 Maintaining Dynamically Created Objects -Dynamically created objects may be used the same as other objects, however they -will not have an id in QML. +When managing dynamically created items, you must ensure the creation context +outlives the created item. Otherwise, if the creation context is destroyed first, +the bindings in the dynamic item will no longer work. + +The actual creation context depends on how an item is created: + +\list +\o If \l {Qt.createComponent(url file)}{Qt.createComponent()} is used, the creation context + is the QDeclarativeContext in which this method is called +\o If \l{Qt.createQmlObject(string qml, object parent, string filepath)}{Qt.createQmlObject()} + if called, it is the context of the item used as the second argument to this method +\o If a \c {Component \{\}} item is defined and \c {Component::}{createObject()} is called, + it is the context in which the \c Component item is defined + +Also, note that while dynamically created objects may be used the same as other objects, they +do not have an id in QML. A restriction which you need to manage with dynamically created items, is that the creation context must outlive the @@ -134,7 +146,7 @@ a worthwhile performance benefit. Note that you should never manually delete items which were dynamically created by QML Elements such as \l{Loader}. To manually delete a QML item, call its destroy method. This method has one -argument, which is an approximate delay in ms and which defaults to zero. This +argument, which is an approximate delay in milliseconds and which defaults to zero. This allows you to wait until the completion of an animation or transition. An example: \code @@ -153,8 +165,9 @@ allows you to wait until the completion of an animation or transition. An exampl object.parent = parentItem; } \endcode -In the above example, the dynamically created rectangle calls destroy as soon as it's created, - but delays long enough for its fade out animation to play. + +In the above example, the dynamically created rectangle calls destroy as soon as it is created, + but delays long enough for its fade out animation to be played. */ diff --git a/doc/src/declarative/globalobject.qdoc b/doc/src/declarative/globalobject.qdoc index 85cbde2..47460cf 100644 --- a/doc/src/declarative/globalobject.qdoc +++ b/doc/src/declarative/globalobject.qdoc @@ -234,58 +234,58 @@ The following functions on the global object allow you to dynamically create QML items from files or strings. See \l{Dynamic Object Management} for an overview of their use. + \section2 Qt.createComponent(url file) - This function takes the URL of a QML file as its only argument. It returns - a component object which can be used to create and load that QML file. - Here is an example. Remember that QML files that might be loaded - over the network cannot be expected to be ready immediately. +This function takes the URL of a QML file as its only argument. It returns +a component object which can be used to create and load that QML file. + +Here is an example. Remember that QML files that might be loaded +over the network cannot be expected to be ready immediately. - \snippet doc/src/snippets/declarative/componentCreation.js 0 - \codeline - \snippet doc/src/snippets/declarative/componentCreation.js 1 - \snippet doc/src/snippets/declarative/componentCreation.js 2 - \snippet doc/src/snippets/declarative/componentCreation.js 4 - \codeline - \snippet doc/src/snippets/declarative/componentCreation.js 5 +\snippet doc/src/snippets/declarative/componentCreation.js 0 +\codeline +\snippet doc/src/snippets/declarative/componentCreation.js 1 - If you are certain the files will be local, you could simplify to: +If you are certain the files will be local, you could simplify to: - \snippet doc/src/snippets/declarative/componentCreation.js 3 +\snippet doc/src/snippets/declarative/componentCreation.js 2 - The methods and properties of the Component element are defined in its own - page, but when using it dynamically only two methods are usually used. - \c Component.createObject() returns the created object or \c null if there is an error. - If there is an error, \l {Component::errorsString()}{Component.errorsString()} describes - the error that occurred. +The methods and properties of the Component element are defined in its own +page, but when using it dynamically only two methods are usually used. +\c Component.createObject() returns the created object or \c null if there is an error. +If there is an error, \l {Component::errorsString()}{Component.errorsString()} describes +the error that occurred. + +If you want to just create an arbitrary string of QML, instead of +loading a QML file, consider the \l{Qt.createQmlObject(string qml, object parent, string filepath)}{Qt.createQmlObject()} function. - If you want to just create an arbitrary string of QML, instead of - loading a QML file, consider the \l{Qt.createQmlObject(string qml, object parent, string filepath)}{Qt.createQmlObject()} function. \section2 Qt.createQmlObject(string qml, object parent, string filepath) - Creates a new object from the specified string of QML. It requires a - second argument, which is the id of an existing QML object to use as - the new object's parent. If a third argument is provided, this is used - for error reporting as the filepath that the QML came from. - Example (where \c targetItem is the id of an existing QML item): +Creates a new object from the specified string of QML. It requires a +second argument, which is the id of an existing QML object to use as +the new object's parent. If a third argument is provided, this is used +for error reporting as the filepath that the QML came from. + +Example (where \c targetItem is the id of an existing QML item): - \snippet doc/src/snippets/declarative/createQmlObject.qml 0 +\snippet doc/src/snippets/declarative/createQmlObject.qml 0 - This function is intended for use inside QML only. It is intended to behave - similarly to eval, but for creating QML elements. +This function is intended for use inside QML only. It is intended to behave +similarly to eval, but for creating QML elements. - Returns the created object, \c or null if there is an error. In the case of an - error, a QtScript Error object is thrown. This object has the additional property, - qmlErrors, which is an array of all the errors encountered when trying to execute the - QML. Each object in the array has the members \c lineNumber, \c columnNumber, \c fileName and \c message. +Returns the created object, \c or null if there is an error. In the case of an +error, a QtScript Error object is thrown. This object has the additional property, +qmlErrors, which is an array of all the errors encountered when trying to execute the +QML. Each object in the array has the members \c lineNumber, \c columnNumber, \c fileName and \c message. - Note that this function returns immediately, and therefore may not work if - the QML loads new components. If you are trying to load a new component, - for example from a QML file, consider the \l{Qt.createComponent(url file)}{Qt.createComponent()} function - instead. 'New components' refers to external QML files that have not yet - been loaded, and so it is safe to use \c Qt.createQmlObject() to load built-in - components. +Note that this function returns immediately, and therefore may not work if +the QML loads new components. If you are trying to load a new component, +for example from a QML file, consider the \l{Qt.createComponent(url file)}{Qt.createComponent()} function +instead. 'New components' refers to external QML files that have not yet +been loaded, and so it is safe to use \c Qt.createQmlObject() to load built-in +components. \section1 XMLHttpRequest diff --git a/doc/src/snippets/declarative/componentCreation.js b/doc/src/snippets/declarative/componentCreation.js index 814545e..be928f0 100644 --- a/doc/src/snippets/declarative/componentCreation.js +++ b/doc/src/snippets/declarative/componentCreation.js @@ -20,38 +20,32 @@ function finishCreation() { } //![0] -//![1] function createSpriteObjects() { -//![1] - - //![2] - component = Qt.createComponent("Sprite.qml"); - if (component.status == Component.Ready) - finishCreation(); - else - component.statusChanged.connect(finishCreation); - //![2] - //![3] - component = Qt.createComponent("Sprite.qml"); - sprite = component.createObject(); +//![1] +component = Qt.createComponent("Sprite.qml"); +if (component.status == Component.Ready) + finishCreation(); +else + component.statusChanged.connect(finishCreation); +//![1] - if (sprite == null) { - // Error Handling - console.log("Error loading component:", component.errorsString()); - } else { - sprite.parent = appWindow; - sprite.x = 100; - sprite.y = 100; - // ... - } - //![3] +//![2] +component = Qt.createComponent("Sprite.qml"); +sprite = component.createObject(); + +if (sprite == null) { + // Error Handling + console.log("Error loading component:", component.errorsString()); +} else { + sprite.parent = appWindow; + sprite.x = 100; + sprite.y = 100; + // ... +} +//![2] -//![4] } -//![4] -//![5] createSpriteObjects(); -//![5] diff --git a/src/declarative/qml/qdeclarativecomponent.cpp b/src/declarative/qml/qdeclarativecomponent.cpp index 6ebf470..3ca0707 100644 --- a/src/declarative/qml/qdeclarativecomponent.cpp +++ b/src/declarative/qml/qdeclarativecomponent.cpp @@ -547,7 +547,11 @@ QDeclarativeComponent::QDeclarativeComponent(QDeclarativeComponentPrivate &dd, Q \qmlmethod object Component::createObject() Returns an object instance from this component, or null if object creation fails. - The object will be created in the same context as the component was created in. + The object will be created in the same context as the one in which the component + was created. + + Note that if the returned object is to be displayed, its \c parent must be set to + an existing item in a scene, or else the object will not be visible. */ /*! -- cgit v0.12 From e4b865ef512e338cf495226dc98540f45fba1d9f Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Fri, 30 Apr 2010 15:42:02 +1000 Subject: More doc fixes --- doc/src/declarative/dynamicobjects.qdoc | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/doc/src/declarative/dynamicobjects.qdoc b/doc/src/declarative/dynamicobjects.qdoc index c376266..5cdd768 100644 --- a/doc/src/declarative/dynamicobjects.qdoc +++ b/doc/src/declarative/dynamicobjects.qdoc @@ -68,7 +68,7 @@ the \l {Qt.createComponent(url file)}{Qt.createComponent()} function on the \l{Q This function takes the URL of the QML file as its only argument and returns a component object which can be used to create and load that QML file. -Once you have a component you can use its \c createObject() method to create an instance of +Once you have a component you can use its \l {Component::createObject()}{createObject()} method to create an instance of the component. Here is an example. Here is a \c Sprite.qml, which defines a simple QML component: @@ -123,19 +123,13 @@ The actual creation context depends on how an item is created: is the QDeclarativeContext in which this method is called \o If \l{Qt.createQmlObject(string qml, object parent, string filepath)}{Qt.createQmlObject()} if called, it is the context of the item used as the second argument to this method -\o If a \c {Component \{\}} item is defined and \c {Component::}{createObject()} is called, - it is the context in which the \c Component item is defined +\o If a \c {Component{}} item is defined and \l {Component::createObject()}{createObject()} + is called on that item, it is the context in which the \c Component is defined +\endlist Also, note that while dynamically created objects may be used the same as other objects, they do not have an id in QML. -A restriction which you need to manage with dynamically created items, -is that the creation context must outlive the -created item. The creation context is the QDeclarativeContext in which \c Qt.createComponent() -was called, or the context in which the Component element, or the item used as the -second argument to \c Qt.createQmlObject(), was specified. If the creation -context is destroyed before the dynamic item is, then bindings in the dynamic item will -fail to work. \section1 Deleting Objects Dynamically You should generally avoid dynamically deleting objects that you did not -- cgit v0.12 From deb92c796c727c6ad0eaf28929cda6d000c1b3c1 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Fri, 30 Apr 2010 15:48:13 +1000 Subject: Fix assignment of value types to javascript var. Make sure the scriptclass is used. Task-number: QTBUG-10329 Reviewed-by: Aaron Kennedy --- src/declarative/qml/qdeclarativeengine.cpp | 8 ++++---- .../qdeclarativevaluetypes/data/varAssignment.qml | 14 ++++++++++++++ .../qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 tests/auto/declarative/qdeclarativevaluetypes/data/varAssignment.qml diff --git a/src/declarative/qml/qdeclarativeengine.cpp b/src/declarative/qml/qdeclarativeengine.cpp index 3f4a735..4d23e8f 100644 --- a/src/declarative/qml/qdeclarativeengine.cpp +++ b/src/declarative/qml/qdeclarativeengine.cpp @@ -1075,7 +1075,7 @@ QScriptValue QDeclarativeEnginePrivate::vector(QScriptContext *ctxt, QScriptEngi qsreal x = ctxt->argument(0).toNumber(); qsreal y = ctxt->argument(1).toNumber(); qsreal z = ctxt->argument(2).toNumber(); - return engine->newVariant(qVariantFromValue(QVector3D(x, y, z))); + return QDeclarativeEnginePrivate::get(engine)->scriptValueFromVariant(qVariantFromValue(QVector3D(x, y, z))); } QScriptValue QDeclarativeEnginePrivate::formatDate(QScriptContext*ctxt, QScriptEngine*engine) @@ -1198,7 +1198,7 @@ QScriptValue QDeclarativeEnginePrivate::rect(QScriptContext *ctxt, QScriptEngine if (w < 0 || h < 0) return engine->nullValue(); - return qScriptValueFromValue(engine, qVariantFromValue(QRectF(x, y, w, h))); + return QDeclarativeEnginePrivate::get(engine)->scriptValueFromVariant(qVariantFromValue(QRectF(x, y, w, h))); } QScriptValue QDeclarativeEnginePrivate::point(QScriptContext *ctxt, QScriptEngine *engine) @@ -1207,7 +1207,7 @@ QScriptValue QDeclarativeEnginePrivate::point(QScriptContext *ctxt, QScriptEngin return ctxt->throwError("Qt.point(): Invalid arguments"); qsreal x = ctxt->argument(0).toNumber(); qsreal y = ctxt->argument(1).toNumber(); - return qScriptValueFromValue(engine, qVariantFromValue(QPointF(x, y))); + return QDeclarativeEnginePrivate::get(engine)->scriptValueFromVariant(qVariantFromValue(QPointF(x, y))); } QScriptValue QDeclarativeEnginePrivate::size(QScriptContext *ctxt, QScriptEngine *engine) @@ -1216,7 +1216,7 @@ QScriptValue QDeclarativeEnginePrivate::size(QScriptContext *ctxt, QScriptEngine return ctxt->throwError("Qt.size(): Invalid arguments"); qsreal w = ctxt->argument(0).toNumber(); qsreal h = ctxt->argument(1).toNumber(); - return qScriptValueFromValue(engine, qVariantFromValue(QSizeF(w, h))); + return QDeclarativeEnginePrivate::get(engine)->scriptValueFromVariant(qVariantFromValue(QSizeF(w, h))); } QScriptValue QDeclarativeEnginePrivate::lighter(QScriptContext *ctxt, QScriptEngine *engine) diff --git a/tests/auto/declarative/qdeclarativevaluetypes/data/varAssignment.qml b/tests/auto/declarative/qdeclarativevaluetypes/data/varAssignment.qml new file mode 100644 index 0000000..e4715ab --- /dev/null +++ b/tests/auto/declarative/qdeclarativevaluetypes/data/varAssignment.qml @@ -0,0 +1,14 @@ +import Qt 4.7 + +QtObject { + property int x; + property int y; + property int z; + + Component.onCompleted: { + var vec3 = Qt.vector3d(1, 2, 3); + x = vec3.x; + y = vec3.y; + z = vec3.z; + } +} diff --git a/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp b/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp index c18fbf5..95b9baa 100644 --- a/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp +++ b/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp @@ -84,6 +84,7 @@ private slots: void enums(); void conflictingBindings(); void returnValues(); + void varAssignment(); private: QDeclarativeEngine engine; @@ -911,6 +912,19 @@ void tst_qdeclarativevaluetypes::returnValues() delete object; } +void tst_qdeclarativevaluetypes::varAssignment() +{ + QDeclarativeComponent component(&engine, TEST_FILE("varAssignment.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("x").toInt(), 1); + QCOMPARE(object->property("y").toInt(), 2); + QCOMPARE(object->property("z").toInt(), 3); + + delete object; +} + QTEST_MAIN(tst_qdeclarativevaluetypes) #include "tst_qdeclarativevaluetypes.moc" -- cgit v0.12