From 895ec534acb63eacd625e2f320cb266dbff3fdb3 Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Tue, 26 Jun 2012 09:47:43 +1000 Subject: [PATCH] Add warning if defining a ListModel with no roles. 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 --- src/qml/qml/qquicklistmodel.cpp | 6 +++ .../qml/qquicklistmodel/tst_qquicklistmodel.cpp | 37 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 0 deletions(-) diff --git a/src/qml/qml/qquicklistmodel.cpp b/src/qml/qml/qquicklistmodel.cpp index 52ef2ab..03f9e3a 100644 --- a/src/qml/qml/qquicklistmodel.cpp +++ b/src/qml/qml/qquicklistmodel.cpp @@ -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 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) diff --git a/tests/auto/qml/qquicklistmodel/tst_qquicklistmodel.cpp b/tests/auto/qml/qquicklistmodel/tst_qquicklistmodel.cpp index 89e8886..e94fe81 100644 --- a/tests/auto/qml/qquicklistmodel/tst_qquicklistmodel.cpp +++ b/tests/auto/qml/qquicklistmodel/tst_qquicklistmodel.cpp @@ -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("qml"); + QTest::addColumn("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" -- 1.7.2.5