Add warning if defining a ListModel with no roles.
authorGlenn Watson <glenn.watson@nokia.com>
Mon, 25 Jun 2012 23:47:43 +0000 (09:47 +1000)
committerQt by Nokia <qt-info@nokia.com>
Tue, 26 Jun 2012 02:29:34 +0000 (04:29 +0200)
When static ListElement types are declared, the role names are
inferred from them at compile time. If they are all empty, it's
not possible to add roles to the model, so warn the user of this
case.

Task-number: QTBUG-21438
Change-Id: Ib4ac30e160c44a5a57ebd1c49fccc2b3db5f0977
Reviewed-by: Bea Lam <bea.lam@nokia.com>
src/qml/qml/qquicklistmodel.cpp
tests/auto/qml/qquicklistmodel/tst_qquicklistmodel.cpp

index 52ef2ab..03f9e3a 100644 (file)
@@ -2327,6 +2327,8 @@ void QQuickListModelParser::setCustomData(QObject *obj, const QByteArray &d)
     const ListModelData *lmd = (const ListModelData *)d.constData();
     const char *data = ((const char *)lmd) + lmd->dataOffset;
 
+    bool setRoles = false;
+
     QStack<DataStackElement> stack;
 
     for (int ii = 0; ii < lmd->instrCount; ++ii) {
@@ -2398,6 +2400,7 @@ void QQuickListModelParser::setCustomData(QObject *obj, const QByteArray &d)
                 }
 
                 e1.model->setOrCreateProperty(e1.elementIndex, name, value);
+                setRoles = true;
             }
             break;
 
@@ -2410,6 +2413,9 @@ void QQuickListModelParser::setCustomData(QObject *obj, const QByteArray &d)
             break;
         }
     }
+
+    if (setRoles == false)
+        qmlInfo(obj) << "All ListElement declarations are empty, no roles can be created unless dynamicRoles is set.";
 }
 
 bool QQuickListModelParser::definesEmptyList(const QString &s)
index 89e8886..e94fe81 100644 (file)
@@ -123,6 +123,8 @@ private slots:
     void role_mode_data();
     void role_mode();
     void string_to_list_crash();
+    void empty_element_warning();
+    void empty_element_warning_data();
 };
 
 bool tst_qquicklistmodel::compareVariantList(const QVariantList &testList, QVariant object)
@@ -1189,6 +1191,41 @@ void tst_qquicklistmodel::string_to_list_crash()
     e.evaluate();
 }
 
+void tst_qquicklistmodel::empty_element_warning_data()
+{
+    QTest::addColumn<QString>("qml");
+    QTest::addColumn<bool>("warning");
+
+    QTest::newRow("empty") << "import QtQuick 2.0\nListModel {}" << false;
+    QTest::newRow("withid") << "import QtQuick 2.0\nListModel { id: model }" << false;
+    QTest::newRow("emptyElement") << "import QtQuick 2.0\nListModel { ListElement {} }" << true;
+    QTest::newRow("emptyElements") << "import QtQuick 2.0\nListModel { ListElement {} ListElement {} }" << true;
+    QTest::newRow("role1") << "import QtQuick 2.0\nListModel { ListElement {a:1} }" << false;
+    QTest::newRow("role2") << "import QtQuick 2.0\nListModel { ListElement {} ListElement {a:1} ListElement {} }" << false;
+    QTest::newRow("role3") << "import QtQuick 2.0\nListModel { ListElement {} ListElement {a:1} ListElement {b:2} }" << false;
+}
+
+void tst_qquicklistmodel::empty_element_warning()
+{
+    QFETCH(QString, qml);
+    QFETCH(bool, warning);
+
+    if (warning) {
+        QString warningString = QLatin1String("file:dummy.qml:2:1: QML ListModel: All ListElement declarations are empty, no roles can be created unless dynamicRoles is set.");
+        QTest::ignoreMessage(QtWarningMsg, warningString.toLatin1());
+    }
+
+    QQmlEngine engine;
+    QQmlComponent component(&engine);
+    component.setData(qml.toUtf8(), QUrl::fromLocalFile(QString("dummy.qml")));
+    QVERIFY(!component.isError());
+
+    QObject *obj = component.create();
+    QVERIFY(obj != 0);
+
+    delete obj;
+}
+
 QTEST_MAIN(tst_qquicklistmodel)
 
 #include "tst_qquicklistmodel.moc"