From a673fb7120a597caf92406007d68eea63e646fed Mon Sep 17 00:00:00 2001
From: Kai Koehne <kai.koehne@nokia.com>
Date: Wed, 3 Mar 2010 11:44:07 +0100
Subject: [PATCH] QmlDesigner: Fix crash when root item of .qml file not
 QDeclarativeItem based

Task-number: BAUHAUS-424
Reviewed-by: Lasse Holmstedt
---
 .../components/stateseditor/stateseditorview.cpp  | 15 +++++++--------
 .../stateseditor/stateseditorwidget.cpp           |  6 +++++-
 tests/manual/qml/testfiles/listmodel.qml          | 10 ++++++++++
 3 files changed, 22 insertions(+), 9 deletions(-)
 create mode 100644 tests/manual/qml/testfiles/listmodel.qml

diff --git a/src/plugins/qmldesigner/components/stateseditor/stateseditorview.cpp b/src/plugins/qmldesigner/components/stateseditor/stateseditorview.cpp
index 5835c0cc6f9..bc83beae3b2 100644
--- a/src/plugins/qmldesigner/components/stateseditor/stateseditorview.cpp
+++ b/src/plugins/qmldesigner/components/stateseditor/stateseditorview.cpp
@@ -59,7 +59,7 @@ void StatesEditorView::setCurrentStateSilent(int index)
     if (debug)
         qDebug() << __FUNCTION__ << index;
 
-    Q_ASSERT(index >= 0 && index <= m_modelStates.count());
+    Q_ASSERT(index >= 0 && index < m_modelStates.count());
 
     // TODO
     QmlModelState state(m_modelStates.at(index));
@@ -75,8 +75,7 @@ void StatesEditorView::setCurrentState(int index)
     if (debug)
         qDebug() << __FUNCTION__ << index;
 
-    if (!(index >= 0 && index <= m_modelStates.count()))
-        Q_ASSERT(index >= 0 && index <= m_modelStates.count());
+    Q_ASSERT(index >= 0 && index < m_modelStates.count());
 
     if (m_modelStates.indexOf(currentState()) == index)
         return;
@@ -167,17 +166,17 @@ void StatesEditorView::modelAttached(Model *model)
     QmlModelView::modelAttached(model);
     Q_ASSERT(m_editorModel->rowCount(QModelIndex()) == 0);
 
-    // find top level states
-    m_stateRootNode = QmlItemNode(rootModelNode());
-    if (!m_stateRootNode.isValid())
-        return;
-
     clearModelStates();
 
     // Add base state
     m_modelStates.insert(0, baseState());
     m_editorModel->insertState(0, baseState().name());
 
+    // find top level states
+    m_stateRootNode = QmlItemNode(rootModelNode());
+    if (!m_stateRootNode.isValid())
+        return;
+
     // Add custom states
     for (int i = 0; i < m_stateRootNode.states().allStates().size(); ++i) {
         QmlModelState state = QmlItemNode(rootModelNode()).states().allStates().at(i);
diff --git a/src/plugins/qmldesigner/components/stateseditor/stateseditorwidget.cpp b/src/plugins/qmldesigner/components/stateseditor/stateseditorwidget.cpp
index 87b78459629..0f82ff95fda 100644
--- a/src/plugins/qmldesigner/components/stateseditor/stateseditorwidget.cpp
+++ b/src/plugins/qmldesigner/components/stateseditor/stateseditorwidget.cpp
@@ -120,7 +120,11 @@ void StatesEditorWidgetPrivate::currentStateChanged()
 
 void StatesEditorWidgetPrivate::addState()
 {
-    QStringList modelStateNames =  statesEditorView->stateRootNode().states().names();
+    // can happen when root node is e.g. a ListModel
+    if (!statesEditorView->stateRootNode().isValid())
+        return;
+
+    QStringList modelStateNames = statesEditorView->stateRootNode().states().names();
 
     QString newStateName;
     int index = 1;
diff --git a/tests/manual/qml/testfiles/listmodel.qml b/tests/manual/qml/testfiles/listmodel.qml
new file mode 100644
index 00000000000..eaf6c60396b
--- /dev/null
+++ b/tests/manual/qml/testfiles/listmodel.qml
@@ -0,0 +1,10 @@
+import Qt 4.6
+
+ListModel {
+    id: myModel
+    ListElement {
+        content: "foo"
+        text: "bar"
+    }
+
+}
-- 
GitLab