diff options
Diffstat (limited to 'tests/auto/declarative/qdeclarativevisualdatamodel')
3 files changed, 104 insertions, 0 deletions
diff --git a/tests/auto/declarative/qdeclarativevisualdatamodel/data/singlerole1.qml b/tests/auto/declarative/qdeclarativevisualdatamodel/data/singlerole1.qml new file mode 100644 index 0000000..7ea74f2 --- /dev/null +++ b/tests/auto/declarative/qdeclarativevisualdatamodel/data/singlerole1.qml @@ -0,0 +1,11 @@ +import Qt 4.7 + +ListView { + width: 100 + height: 100 + anchors.fill: parent + model: myModel + delegate: Component { + Text { objectName: "name"; text: name } + } +} diff --git a/tests/auto/declarative/qdeclarativevisualdatamodel/data/singlerole2.qml b/tests/auto/declarative/qdeclarativevisualdatamodel/data/singlerole2.qml new file mode 100644 index 0000000..6654d6b --- /dev/null +++ b/tests/auto/declarative/qdeclarativevisualdatamodel/data/singlerole2.qml @@ -0,0 +1,11 @@ +import Qt 4.7 + +ListView { + width: 100 + height: 100 + anchors.fill: parent + model: myModel + delegate: Component { + Text { objectName: "name"; text: modelData } + } +} diff --git a/tests/auto/declarative/qdeclarativevisualdatamodel/tst_qdeclarativevisualdatamodel.cpp b/tests/auto/declarative/qdeclarativevisualdatamodel/tst_qdeclarativevisualdatamodel.cpp index 95ef4fc..d73a872 100644 --- a/tests/auto/declarative/qdeclarativevisualdatamodel/tst_qdeclarativevisualdatamodel.cpp +++ b/tests/auto/declarative/qdeclarativevisualdatamodel/tst_qdeclarativevisualdatamodel.cpp @@ -75,6 +75,39 @@ static void initStandardTreeModel(QStandardItemModel *model) model->insertRow(2, item); } +class SingleRoleModel : public QAbstractListModel +{ + Q_OBJECT + +public: + SingleRoleModel(QObject *parent = 0) { + QHash<int, QByteArray> roles; + roles.insert(Qt::DisplayRole , "name"); + setRoleNames(roles); + list << "one" << "two" << "three" << "four"; + } + +public slots: + void set(int idx, QString string) { + list[idx] = string; + emit dataChanged(index(idx,0), index(idx,0)); + } + +protected: + int rowCount(const QModelIndex &parent = QModelIndex()) const { + return list.count(); + } + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const { + if (role == Qt::DisplayRole) + return list.at(index.row()); + return QVariant(); + } + +private: + QStringList list; +}; + + class tst_qdeclarativevisualdatamodel : public QObject { Q_OBJECT @@ -86,6 +119,7 @@ private slots: void updateLayout(); void childChanged(); void objectListModel(); + void singleRole(); private: QDeclarativeEngine engine; @@ -282,6 +316,54 @@ void tst_qdeclarativevisualdatamodel::objectListModel() QCOMPARE(name->text(), QString("Changed")); } +void tst_qdeclarativevisualdatamodel::singleRole() +{ + { + QDeclarativeView view; + + SingleRoleModel model; + + QDeclarativeContext *ctxt = view.rootContext(); + ctxt->setContextProperty("myModel", &model); + + view.setSource(QUrl::fromLocalFile(SRCDIR "/data/singlerole1.qml")); + + QDeclarativeListView *listview = qobject_cast<QDeclarativeListView*>(view.rootObject()); + QVERIFY(listview != 0); + + QDeclarativeItem *contentItem = listview->contentItem(); + QVERIFY(contentItem != 0); + + QDeclarativeText *name = findItem<QDeclarativeText>(contentItem, "name", 1); + QCOMPARE(name->text(), QString("two")); + + model.set(1, "Changed"); + QCOMPARE(name->text(), QString("Changed")); + } + { + QDeclarativeView view; + + SingleRoleModel model; + + QDeclarativeContext *ctxt = view.rootContext(); + ctxt->setContextProperty("myModel", &model); + + view.setSource(QUrl::fromLocalFile(SRCDIR "/data/singlerole2.qml")); + + QDeclarativeListView *listview = qobject_cast<QDeclarativeListView*>(view.rootObject()); + QVERIFY(listview != 0); + + QDeclarativeItem *contentItem = listview->contentItem(); + QVERIFY(contentItem != 0); + + QDeclarativeText *name = findItem<QDeclarativeText>(contentItem, "name", 1); + QCOMPARE(name->text(), QString("two")); + + model.set(1, "Changed"); + QCOMPARE(name->text(), QString("Changed")); + } +} + template<typename T> T *tst_qdeclarativevisualdatamodel::findItem(QGraphicsObject *parent, const QString &objectName, int index) { |