From fa8a4beb157f82e02cf8471ade7da0faee7a9fa1 Mon Sep 17 00:00:00 2001
From: Alan Alpert <aalpert@rim.com>
Date: Tue, 26 Feb 2013 13:50:51 -0800
Subject: [PATCH] Fix remote image loading for AnimatedSprite

The Sprite generated behind the scenes had no QmlEngine associated, and
the engine is needed by QQuickPixmap for async loading.

Task-number: QTBUG-28086
Change-Id: Ibf3b03c54b339fe8f44201dc6fcb507e5274bbec
Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
---
 src/quick/items/qquickanimatedsprite.cpp |  4 ++--
 src/quick/items/qquicksprite.cpp         | 11 +++++++++--
 src/quick/items/qquickspriteengine.cpp   |  1 +
 3 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/quick/items/qquickanimatedsprite.cpp b/src/quick/items/qquickanimatedsprite.cpp
index 343cac5d5c..f09e9bb90e 100644
--- a/src/quick/items/qquickanimatedsprite.cpp
+++ b/src/quick/items/qquickanimatedsprite.cpp
@@ -365,7 +365,7 @@ QQuickAnimatedSprite::QQuickAnimatedSprite(QQuickItem *parent) :
     QQuickItem(parent)
     , m_node(0)
     , m_material(0)
-    , m_sprite(new QQuickSprite)
+    , m_sprite(new QQuickSprite(this))
     , m_spriteEngine(0)
     , m_curFrame(0)
     , m_pleaseReset(false)
@@ -540,7 +540,7 @@ QSGGeometryNode* QQuickAnimatedSprite::buildNode()
 
     m_material = new QQuickAnimatedSpriteMaterial();
 
-    QImage image = m_spriteEngine->assembledImage();
+    QImage image = m_spriteEngine->assembledImage(); //Engine prints errors if there are any
     if (image.isNull())
         return 0;
     m_sheetSize = QSizeF(image.size());
diff --git a/src/quick/items/qquicksprite.cpp b/src/quick/items/qquicksprite.cpp
index 13593384d8..b4138308f6 100644
--- a/src/quick/items/qquicksprite.cpp
+++ b/src/quick/items/qquicksprite.cpp
@@ -254,8 +254,15 @@ int QQuickSprite::variedDuration() const //Deals with precedence when multiple d
 void QQuickSprite::startImageLoading()
 {
     m_pix.clear(this);
-    if (!m_source.isEmpty())
-        m_pix.load(qmlEngine(this), m_source);
+    if (!m_source.isEmpty()) {
+        QQmlEngine *e = qmlEngine(this);
+        if (!e) { //If not created in QML, you must set the QObject parent to the QML element so this can work
+            e = qmlEngine(parent());
+            if (!e)
+                qWarning() << "QQuickSprite: Cannot find QQmlEngine - this class is only for use in QML and may not work";
+        }
+        m_pix.load(e, m_source);
+    }
 }
 
 QT_END_NAMESPACE
diff --git a/src/quick/items/qquickspriteengine.cpp b/src/quick/items/qquickspriteengine.cpp
index 4c7be3bce9..aa93d31cf7 100644
--- a/src/quick/items/qquickspriteengine.cpp
+++ b/src/quick/items/qquickspriteengine.cpp
@@ -328,6 +328,7 @@ QQuickPixmap::Status QQuickSpriteEngine::status()//Composed status of all Sprite
     null = loading = ready = 0;
     foreach (QQuickSprite* s, m_sprites) {
         switch (s->m_pix.status()) {
+            // ### Maybe add an error message here, because this null shouldn't be reached but when it does, the image fails without an error message.
             case QQuickPixmap::Null : null++; break;
             case QQuickPixmap::Loading : loading++; break;
             case QQuickPixmap::Error : return QQuickPixmap::Error;
-- 
GitLab