diff --git a/src/plugins/qmljsinspector/qmljsclientproxy.cpp b/src/plugins/qmljsinspector/qmljsclientproxy.cpp
index 0c5996c0703ce42019ed42eee40cb777bbfd4b45..e754d8075b1c389f019e46759e6b95e661a0941b 100644
--- a/src/plugins/qmljsinspector/qmljsclientproxy.cpp
+++ b/src/plugins/qmljsinspector/qmljsclientproxy.cpp
@@ -119,7 +119,7 @@ void ClientProxy::refreshObjectTree()
         m_objectTreeQuery = m_client->queryObjectRecursive(m_rootObject, this);
 
         if (!m_objectTreeQuery->isWaiting()) {
-            objectTreeFetched();
+            objectTreeFetched(m_objectTreeQuery->state());
         } else {
             connect(m_objectTreeQuery,
                     SIGNAL(stateChanged(QDeclarativeDebugQuery::State)),
@@ -143,12 +143,13 @@ void ClientProxy::onCurrentObjectsChanged(const QList<int> &debugIds)
             // So the only choice that remains is to update the complete tree when we have an unknown debug id.
             if (!m_objectTreeQuery)
                 m_objectTreeQuery = m_client->queryObjectRecursive(m_rootObject, this);
+            break;
         }
     }
 
     if (m_objectTreeQuery) {
         if (!m_objectTreeQuery->isWaiting()) {
-            objectTreeFetched();
+            objectTreeFetched(m_objectTreeQuery->state());
         } else {
             connect(m_objectTreeQuery,
                     SIGNAL(stateChanged(QDeclarativeDebugQuery::State)),
@@ -222,6 +223,7 @@ void ClientProxy::connectionStateChanged()
                         SIGNAL(animationSpeedChanged(qreal)), SIGNAL(animationSpeedChanged(qreal)));
                 connect(m_designClient,
                         SIGNAL(designModeBehaviorChanged(bool)), SIGNAL(designModeBehaviorChanged(bool)));
+                connect(m_designClient, SIGNAL(reloaded()), this, SIGNAL(serverReloaded()));
             }
 
             (void) new DebuggerClient(m_conn);
@@ -373,6 +375,11 @@ void ClientProxy::contextChanged()
 
 void ClientProxy::objectTreeFetched(QDeclarativeDebugQuery::State state)
 {
+    if (state == QDeclarativeDebugQuery::Error) {
+        delete m_objectTreeQuery;
+        m_objectTreeQuery = 0;
+    }
+
     if (state != QDeclarativeDebugQuery::Completed) {
         m_rootObject = QDeclarativeDebugObjectReference();
         return;
diff --git a/src/plugins/qmljsinspector/qmljsclientproxy.h b/src/plugins/qmljsinspector/qmljsclientproxy.h
index 6a205ca57ee459e46a838f48398e3313f6f620b7..98db3616c796df5849dec8e20df499fa07951243 100644
--- a/src/plugins/qmljsinspector/qmljsclientproxy.h
+++ b/src/plugins/qmljsinspector/qmljsclientproxy.h
@@ -93,6 +93,7 @@ signals:
     void zoomToolActivated();
     void animationSpeedChanged(qreal slowdownFactor);
     void designModeBehaviorChanged(bool inDesignMode);
+    void serverReloaded();
 
 public slots:
     void queryEngineContext(int id);
diff --git a/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp b/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp
index eb5315be280522d76760053e72b100bf61777c72..85d775f3c35914cbe90a77deca6bb3a239cea688 100644
--- a/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp
+++ b/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp
@@ -96,6 +96,8 @@ void QmlJSDesignDebugClient::messageReceived(const QByteArray &message)
         bool inDesignMode;
         ds >> inDesignMode;
         emit designModeBehaviorChanged(inDesignMode);
+    } else if (type == "RELOADED") {
+        emit reloaded();
     }
 }
 
diff --git a/src/plugins/qmljsinspector/qmljsdesigndebugclient.h b/src/plugins/qmljsinspector/qmljsdesigndebugclient.h
index a37a8745d2ebf1f83d382b11fbbbe9a94a66d24d..e49b3587c077c9396427005eb01f99be39a88e15 100644
--- a/src/plugins/qmljsinspector/qmljsdesigndebugclient.h
+++ b/src/plugins/qmljsinspector/qmljsdesigndebugclient.h
@@ -81,6 +81,7 @@ signals:
     void zoomToolActivated();
     void animationSpeedChanged(qreal slowdownFactor);
     void designModeBehaviorChanged(bool inDesignMode);
+    void reloaded(); // the server has reloaded the document
 
 protected:
     virtual void messageReceived(const QByteArray &);
diff --git a/src/plugins/qmljsinspector/qmljsinspector.cpp b/src/plugins/qmljsinspector/qmljsinspector.cpp
index f1a76c3556f74d00d47364d9b7ea0b1951db7994..fee3bfa102c7f072fb94f8bda22bd832532d3b94 100644
--- a/src/plugins/qmljsinspector/qmljsinspector.cpp
+++ b/src/plugins/qmljsinspector/qmljsinspector.cpp
@@ -139,6 +139,7 @@ Inspector::Inspector(QObject *parent)
     connect(m_clientProxy, SIGNAL(aboutToReloadEngines()), SLOT(aboutToReloadEngines()));
     connect(m_clientProxy, SIGNAL(enginesChanged()), SLOT(updateEngineList()));
     connect(m_clientProxy, SIGNAL(aboutToDisconnect()), SLOT(disconnectWidgets()));
+    connect(m_clientProxy, SIGNAL(serverReloaded()), this, SLOT(serverReloaded()));
 
     connect(Debugger::DebuggerPlugin::instance(),
             SIGNAL(stateChanged(int)), this, SLOT(debuggerStateChanged(int)));
@@ -224,21 +225,41 @@ void Inspector::initializeDocuments()
     if (!modelManager())
         return;
 
-    QmlJS::Snapshot snapshot = modelManager()->snapshot();
+    m_loadedSnapshot = modelManager()->snapshot();
     Core::EditorManager *em = Core::EditorManager::instance();
     connect(em, SIGNAL(editorAboutToClose(Core::IEditor*)), SLOT(removePreviewForEditor(Core::IEditor*)));
     connect(em, SIGNAL(editorOpened(Core::IEditor*)), SLOT(createPreviewForEditor(Core::IEditor*)));
 
     // initial update
-    foreach (QmlJS::Document::Ptr doc, snapshot) {
-        QmlJSLiveTextPreview *preview = new QmlJSLiveTextPreview(doc, this);
+#if 0
+    foreach (Core::IEditor *editor, em->openedEditors()) {
+        createPreviewForEditor(editor);
+    }
+#else
+    foreach (QmlJS::Document::Ptr doc, m_loadedSnapshot) {
+        QmlJSLiveTextPreview *preview = new QmlJSLiveTextPreview(doc, doc, this);
         connect(preview,
                 SIGNAL(selectedItemsChanged(QList<QDeclarativeDebugObjectReference>)),
                 SLOT(changeSelectedItems(QList<QDeclarativeDebugObjectReference>)));
         m_textPreviews.insert(doc->fileName(), preview);
     }
+#endif
 }
 
+void Inspector::serverReloaded()
+{
+    QmlJS::Snapshot snapshot = modelManager()->snapshot();
+    m_loadedSnapshot = snapshot;
+    for (QHash<QString, QmlJSLiveTextPreview *>::const_iterator it = m_textPreviews.constBegin();
+         it != m_textPreviews.constEnd(); ++it) {
+        Document::Ptr doc = snapshot.document(it.key());
+        it.value()->resetInitialDoc(doc);
+    }
+    ClientProxy::instance()->queryEngineContext(0);
+    //ClientProxy::instance()->refreshObjectTree();
+}
+
+
 void Inspector::removePreviewForEditor(Core::IEditor *oldEditor)
 {
     if (QmlJSLiveTextPreview *preview = m_textPreviews.value(oldEditor->file()->fileName())) {
@@ -253,12 +274,14 @@ void Inspector::createPreviewForEditor(Core::IEditor *newEditor)
         QmlJS::Document::Ptr doc = modelManager()->snapshot().document(filename);
         if (!doc || !doc->qmlProgram())
             return;
+        QmlJS::Document::Ptr initdoc = m_loadedSnapshot.document(filename);
+        if (!initdoc)
+            initdoc = doc;
 
         if (m_textPreviews.contains(filename)) {
             m_textPreviews.value(filename)->associateEditor(newEditor);
         } else {
-
-            QmlJSLiveTextPreview *preview = new QmlJSLiveTextPreview(doc, this);
+            QmlJSLiveTextPreview *preview = new QmlJSLiveTextPreview(doc, initdoc, this);
             connect(preview,
                     SIGNAL(selectedItemsChanged(QList<QDeclarativeDebugObjectReference>)),
                     SLOT(changeSelectedItems(QList<QDeclarativeDebugObjectReference>)));
diff --git a/src/plugins/qmljsinspector/qmljsinspector.h b/src/plugins/qmljsinspector/qmljsinspector.h
index dba440d40fde4eca2433b47a7fd23439d4648300..8e93c85aabd641b60fcf9eefa979683cf9f8ee41 100644
--- a/src/plugins/qmljsinspector/qmljsinspector.h
+++ b/src/plugins/qmljsinspector/qmljsinspector.h
@@ -99,6 +99,7 @@ signals:
 public slots:
     void setSimpleDockWidgetArrangement();
     void reloadQmlViewer();
+    void serverReloaded();
 
 private slots:
     void gotoObjectReferenceDefinition(const QDeclarativeDebugObjectReference &obj);
@@ -152,6 +153,7 @@ private:
 
     // Qml/JS integration
     QHash<QString, QmlJSLiveTextPreview *> m_textPreviews;
+    QmlJS::Snapshot m_loadedSnapshot; //the snapshot loaded by the viewer
 };
 
 } // Internal
diff --git a/src/plugins/qmljsinspector/qmljslivetextpreview.cpp b/src/plugins/qmljsinspector/qmljslivetextpreview.cpp
index 0bdc0f12687324904e7af5589165c95532d8bfaf..554ef22750a4a186d1fd713f0d32717f6f0b5a42 100644
--- a/src/plugins/qmljsinspector/qmljslivetextpreview.cpp
+++ b/src/plugins/qmljsinspector/qmljslivetextpreview.cpp
@@ -131,9 +131,10 @@ void QmlJSLiveTextPreview::unassociateEditor(Core::IEditor *oldEditor)
     }
 }
 
-QmlJSLiveTextPreview::QmlJSLiveTextPreview(QmlJS::Document::Ptr doc, QObject *parent) :
-    QObject(parent), m_previousDoc(doc), m_initialDoc(doc)
+QmlJSLiveTextPreview::QmlJSLiveTextPreview(const QmlJS::Document::Ptr &doc, const QmlJS::Document::Ptr &initDoc, QObject* parent) :
+    QObject(parent), m_previousDoc(doc), m_initialDoc(initDoc)
 {
+    Q_ASSERT(doc->fileName() == initDoc->fileName());
     ClientProxy *clientProxy = ClientProxy::instance();
     m_filename = doc->fileName();
 
@@ -151,6 +152,15 @@ QmlJSLiveTextPreview::QmlJSLiveTextPreview(QmlJS::Document::Ptr doc, QObject *pa
         associateEditor(editor);
 }
 
+void QmlJSLiveTextPreview::resetInitialDoc(const QmlJS::Document::Ptr &doc)
+{
+    m_initialDoc = doc;
+    m_previousDoc = doc;
+    m_createdObjects.clear();
+    m_debugIds.clear();
+}
+
+
 QList<QDeclarativeDebugObjectReference > QmlJSLiveTextPreview::objectReferencesForOffset(quint32 offset) const
 {
     QList<QDeclarativeDebugObjectReference > result;
@@ -234,9 +244,11 @@ void QmlJSLiveTextPreview::updateDebugIds(const QDeclarativeDebugObjectReference
         doc->qmlProgram()->accept(&visitor);
 
         m_debugIds = visitor.result;
-        Delta delta;
-        delta.doNotSendChanges = true;
-        m_debugIds = delta(doc, m_previousDoc, m_debugIds);
+        if (doc != m_previousDoc) {
+            Delta delta;
+            delta.doNotSendChanges = true;
+            m_debugIds = delta(doc, m_previousDoc, m_debugIds);
+        }
     }
 
     const QmlJS::Document::Ptr &doc = m_previousDoc;
@@ -264,9 +276,11 @@ void QmlJSLiveTextPreview::updateDebugIds(const QDeclarativeDebugObjectReference
         doc->qmlProgram()->accept(&visitor);
 
         Delta::DebugIdMap debugIds = visitor.result;
-        Delta delta;
-        delta.doNotSendChanges = true;
-        debugIds = delta(doc, m_previousDoc, debugIds);
+        if (doc != m_previousDoc) {
+            Delta delta;
+            delta.doNotSendChanges = true;
+            debugIds = delta(doc, m_previousDoc, debugIds);
+        }
         for(Delta::DebugIdMap::const_iterator it2 = debugIds.constBegin();
             it2 != debugIds.constEnd(); ++it2) {
             m_debugIds[it2.key()] += it2.value();
diff --git a/src/plugins/qmljsinspector/qmljslivetextpreview.h b/src/plugins/qmljsinspector/qmljslivetextpreview.h
index 1f2410a7be6ced63a048f2330c00d197fcf3da1d..420875e45da5e056400d5a1c0b48d7a2705dc3e9 100644
--- a/src/plugins/qmljsinspector/qmljslivetextpreview.h
+++ b/src/plugins/qmljsinspector/qmljslivetextpreview.h
@@ -33,7 +33,7 @@ class QmlJSLiveTextPreview : public QObject
     Q_OBJECT
 
 public:
-    explicit QmlJSLiveTextPreview(QmlJS::Document::Ptr doc, QObject *parent = 0);
+    explicit QmlJSLiveTextPreview(const QmlJS::Document::Ptr &doc, const QmlJS::Document::Ptr &initDoc, QObject *parent = 0);
     static QmlJS::ModelManagerInterface *modelManager();
     //void updateDocuments();
 
@@ -41,6 +41,7 @@ public:
     void unassociateEditor(Core::IEditor *editor);
     void setActiveObject(const QDeclarativeDebugObjectReference &object);
     void mapObjectToQml(const QDeclarativeDebugObjectReference &object);
+    void resetInitialDoc(const QmlJS::Document::Ptr &doc);
 
 signals:
     void selectedItemsChanged(const QList<QDeclarativeDebugObjectReference> &objects);
diff --git a/src/tools/qml/qmlobserver/qdeclarativedesigndebugserver.cpp b/src/tools/qml/qmlobserver/qdeclarativedesigndebugserver.cpp
index 5242826c0fadc824f6dd1f485affb9a2f6c67f85..1d53adf6454386309ea3fc88dbc2cfc9f01bef8d 100644
--- a/src/tools/qml/qmlobserver/qdeclarativedesigndebugserver.cpp
+++ b/src/tools/qml/qmlobserver/qdeclarativedesigndebugserver.cpp
@@ -117,3 +117,14 @@ void QDeclarativeDesignDebugServer::setAnimationSpeed(qreal slowdownFactor)
 
     sendMessage(message);
 }
+
+void QDeclarativeDesignDebugServer::reloaded()
+{
+    QByteArray message;
+    QDataStream ds(&message, QIODevice::WriteOnly);
+
+    ds << QByteArray("RELOADED");
+
+    sendMessage(message);
+}
+
diff --git a/src/tools/qml/qmlobserver/qdeclarativedesigndebugserver.h b/src/tools/qml/qmlobserver/qdeclarativedesigndebugserver.h
index 301229ebb0a28ae4de308096e22abb883b943a2f..a2f2890feb34d0618282f4cdcde4e4f076e8790a 100644
--- a/src/tools/qml/qmlobserver/qdeclarativedesigndebugserver.h
+++ b/src/tools/qml/qmlobserver/qdeclarativedesigndebugserver.h
@@ -62,6 +62,7 @@ public:
     void setCurrentObjects(QList<QObject*> items);
     void setAnimationSpeed(qreal slowdownFactor);
     void setCurrentTool(QmlViewer::Constants::DesignTool toolId);
+    void reloaded();
 
 Q_SIGNALS:
     void currentObjectsChanged(const QList<QObject*> &objects);
diff --git a/src/tools/qml/qmlobserver/qdeclarativedesignview.cpp b/src/tools/qml/qmlobserver/qdeclarativedesignview.cpp
index cbb67dbf6ab03c7e2245404e3b7debd9fe60875f..76de0ab06ef688394984520159f48ee43a6106c8 100644
--- a/src/tools/qml/qmlobserver/qdeclarativedesignview.cpp
+++ b/src/tools/qml/qmlobserver/qdeclarativedesignview.cpp
@@ -202,8 +202,8 @@ void QDeclarativeDesignView::createQmlObject(const QString &qml, QObject *parent
     QObject *newObject = component.create(parentContext);
     if (newObject) {
         newObject->setParent(parent);
-        QDeclarativeItem *parentItem = dynamic_cast<QDeclarativeItem*>(parent);
-        QDeclarativeItem *newItem    = dynamic_cast<QDeclarativeItem*>(newObject);
+        QDeclarativeItem *parentItem = qobject_cast<QDeclarativeItem*>(parent);
+        QDeclarativeItem *newItem    = qobject_cast<QDeclarativeItem*>(newObject);
         if (parentItem && newItem) {
             newItem->setParentItem(parentItem);
         }
@@ -533,6 +533,7 @@ void QDeclarativeDesignView::onStatusChanged(QDeclarativeView::Status status)
             m_subcomponentEditorTool->pushContext(rootObject());
             emit executionStarted(1.0f);
         }
+        qmlDesignDebugServer()->reloaded();
     }
 }