aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qml/qqmltablemodel/data/TestModel.qml21
-rw-r--r--tests/auto/qml/qqmltablemodel/data/builtInRoles.qml47
-rw-r--r--tests/auto/qml/qqmltablemodel/data/common.qml95
-rw-r--r--tests/auto/qml/qqmltablemodel/data/complex.qml (renamed from tests/auto/qml/qqmltablemodel/data/roleDataProvider.qml)59
-rw-r--r--tests/auto/qml/qqmltablemodel/data/dataAndSetData.qml19
-rw-r--r--tests/auto/qml/qqmltablemodel/data/empty.qml36
-rw-r--r--tests/auto/qml/qqmltablemodel/data/omitTableModelColumnIndex.qml (renamed from tests/auto/qml/qqmltablemodel/data/explicitDisplayRole.qml)20
-rw-r--r--tests/auto/qml/qqmltablemodel/data/setDataThroughDelegate.qml25
-rw-r--r--tests/auto/qml/qqmltablemodel/data/setRowsMultipleTimes.qml48
-rw-r--r--tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp713
-rw-r--r--tests/manual/tableview/tablemodel/form/RowForm.qml36
-rw-r--r--tests/manual/tableview/tablemodel/form/main.qml60
12 files changed, 591 insertions, 588 deletions
diff --git a/tests/auto/qml/qqmltablemodel/data/TestModel.qml b/tests/auto/qml/qqmltablemodel/data/TestModel.qml
index 7aeb5d03f4..00e1fa65a7 100644
--- a/tests/auto/qml/qqmltablemodel/data/TestModel.qml
+++ b/tests/auto/qml/qqmltablemodel/data/TestModel.qml
@@ -33,15 +33,18 @@ import "TestUtils.js" as TestUtils
TableModel {
id: testModel
objectName: "testModel"
- roleDataProvider: TestUtils.testModelRoleDataProvider
+
+ TableModelColumn { display: "name" }
+ TableModelColumn { display: "age" }
+
rows: [
- [
- { name: "John" },
- { age: 22 }
- ],
- [
- { name: "Oliver" },
- { age: 33 }
- ]
+ {
+ name: "John",
+ age: 22
+ },
+ {
+ name: "Oliver",
+ age: 33
+ }
]
}
diff --git a/tests/auto/qml/qqmltablemodel/data/builtInRoles.qml b/tests/auto/qml/qqmltablemodel/data/builtInRoles.qml
deleted file mode 100644
index d9882e4dea..0000000000
--- a/tests/auto/qml/qqmltablemodel/data/builtInRoles.qml
+++ /dev/null
@@ -1,47 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2019 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-import Qt.labs.qmlmodels 1.0
-
-import "TestUtils.js" as TestUtils
-
-TableModel {
- id: testModel
- objectName: "testModel"
- roleDataProvider: TestUtils.testModelRoleDataProvider
- rows: [
- [
- { name: "John", someOtherRole1: "foo" }, // column 0
- { age: 22, someOtherRole2: "foo" } // column 1
- ],
- [
- { name: "Oliver", someOtherRole1: "foo" }, // column 0
- { age: 33, someOtherRole2: "foo" } // column 1
- ]
- ]
-}
diff --git a/tests/auto/qml/qqmltablemodel/data/common.qml b/tests/auto/qml/qqmltablemodel/data/common.qml
index aec796bd4f..2f8b0c072b 100644
--- a/tests/auto/qml/qqmltablemodel/data/common.qml
+++ b/tests/auto/qml/qqmltablemodel/data/common.qml
@@ -26,7 +26,7 @@
**
****************************************************************************/
-import QtQuick 2.12
+import QtQuick 2.13
import Qt.labs.qmlmodels 1.0
Item {
@@ -38,80 +38,101 @@ Item {
property alias tableView: tableView
function appendRow(personName, personAge) {
- testModel.appendRow([
- { name: personName },
- { age: personAge }
- ])
+ testModel.appendRow({
+ name: personName,
+ age: personAge
+ })
+ }
+
+ function appendRowExtraData() {
+ testModel.appendRow({
+ name: "Foo",
+ age: 99,
+ nonExistentRole: 123
+ })
}
function appendRowInvalid1() {
- testModel.appendRow([
- { name: "Foo" },
- { age: 99 },
- { nonExistentRole: 123 }
- ])
+ testModel.appendRow(123)
}
function appendRowInvalid2() {
- testModel.appendRow(123)
+ testModel.appendRow({
+ name: "Foo",
+ age: []
+ })
}
function appendRowInvalid3() {
testModel.appendRow([
- { name: "Foo" },
- { age: [] }
+ { name: "Bar" },
+ { age: "111" }
])
}
function insertRow(personName, personAge, rowIndex) {
- testModel.insertRow(rowIndex, [
- { name: personName },
- { age: personAge }]
- )
+ testModel.insertRow(rowIndex, {
+ name: personName,
+ age: personAge
+ })
+ }
+
+ function insertRowExtraData() {
+ testModel.insertRow(0, {
+ name: "Foo",
+ age: 99,
+ nonExistentRole: 123
+ })
}
function insertRowInvalid1() {
- testModel.insertRow(0, [
- { name: "Foo" },
- { age: 99 },
- { nonExistentRole: 123 }
- ])
+ testModel.insertRow(0, 123)
}
function insertRowInvalid2() {
- testModel.insertRow(0, 123)
+ testModel.insertRow(0, {
+ name: "Foo",
+ age: []
+ })
}
function insertRowInvalid3() {
testModel.insertRow(0, [
- { name: "Foo" },
- { age: [] }
+ { name: "Bar" },
+ { age: "111" }
])
}
function setRow(rowIndex, personName, personAge) {
- testModel.setRow(rowIndex, [
- { name: personName },
- { age: personAge }]
- )
+ testModel.setRow(rowIndex, {
+ name: personName,
+ age: personAge
+ })
+ }
+
+ function setRowExtraData() {
+ testModel.setRow(0, {
+ name: "Foo",
+ age: 99,
+ nonExistentRole: 123
+ })
}
function setRowInvalid1() {
- testModel.setRow(0, [
- { name: "Foo" },
- { age: 99 },
- { nonExistentRole: 123 }
- ])
+ testModel.setRow(0, 123)
}
function setRowInvalid2() {
- testModel.setRow(0, 123)
+ testModel.setRow(0, {
+ name: "Foo",
+ age: []
+ })
}
function setRowInvalid3() {
testModel.setRow(0, [
- { name: "Foo" },
- { age: [] }
+ { name: "Bar" },
+ { age: "111" }
])
}
diff --git a/tests/auto/qml/qqmltablemodel/data/roleDataProvider.qml b/tests/auto/qml/qqmltablemodel/data/complex.qml
index 2706ea54fd..dbf53bac7e 100644
--- a/tests/auto/qml/qqmltablemodel/data/roleDataProvider.qml
+++ b/tests/auto/qml/qqmltablemodel/data/complex.qml
@@ -26,40 +26,43 @@
**
****************************************************************************/
-import QtQuick 2.12
+import QtQuick 2.13
import Qt.labs.qmlmodels 1.0
-Item {
- id: root
- width: 200
- height: 200
+TableView {
+ width: 100
+ height: 100
+ delegate: Item {
+ implicitWidth: 50
+ implicitHeight: 50
- property alias testModel: testModel
-
- TableModel {
+ Text {
+ text: model.display
+ anchors.centerIn: parent
+ }
+ }
+ model: TableModel {
id: testModel
objectName: "testModel"
- rows: [
- [ { name: "Rex" }, { age: 3 } ],
- [ { name: "Buster" }, { age: 5 } ]
- ]
- roleDataProvider: function(index, role, cellData) {
- if (role === "display") {
- // Age will now be in dog years
- if (cellData.hasOwnProperty("age"))
- return (cellData.age * 7);
- else if (index.column === 0)
- return (cellData.name);
- }
- return cellData;
+
+ TableModelColumn {
+ display: function(modelIndex) { return testModel.rows[modelIndex.row][0].name }
+ setDisplay: function(modelIndex, cellData) { testModel.rows[modelIndex.row][0].name = cellData }
}
- }
- TableView {
- anchors.fill: parent
- model: testModel
- delegate: Text {
- id: textItem
- text: model.display
+ TableModelColumn {
+ display: function(modelIndex) { return testModel.rows[modelIndex.row][1].age }
+ setDisplay: function(modelIndex, cellData) { testModel.rows[modelIndex.row][1].age = cellData }
}
+
+ rows: [
+ [
+ { name: "John" },
+ { age: 22 }
+ ],
+ [
+ { name: "Oliver" },
+ { age: 33 }
+ ]
+ ]
}
}
diff --git a/tests/auto/qml/qqmltablemodel/data/dataAndSetData.qml b/tests/auto/qml/qqmltablemodel/data/dataAndSetData.qml
index d61c50ba2c..d3f726bfa1 100644
--- a/tests/auto/qml/qqmltablemodel/data/dataAndSetData.qml
+++ b/tests/auto/qml/qqmltablemodel/data/dataAndSetData.qml
@@ -31,36 +31,25 @@ import Qt.labs.qmlmodels 1.0
TableView {
width: 200; height: 200
- model: TableModel {
+ model: TestModel {
id: testModel
- objectName: "testModel"
- rows: [
- [
- { name: "John", someOtherRole1: "foo" }, // column 0
- { age: 22, someOtherRole2: "foo" } // column 1
- ],
- [
- { name: "Oliver", someOtherRole1: "foo" }, // column 0
- { age: 33, someOtherRole2: "foo" } // column 1
- ]
- ]
// This is silly: in real life, store the birthdate instead of the age,
// and let the delegate calculate the age, so it won't need updating
function happyBirthday(dude) {
var row = -1;
for (var r = 0; row < 0 && r < testModel.rowCount; ++r)
- if (testModel.data(testModel.index(r, 0), "name") === dude)
+ if (testModel.data(testModel.index(r, 0), "display") === dude)
row = r;
var index = testModel.index(row, 1)
- testModel.setData(index, "age", testModel.data(index, "age") + 1)
+ testModel.setData(index, "display", testModel.data(index, "display") + 1)
}
}
delegate: Text {
id: textItem
text: model.display
TapHandler {
- onTapped: testModel.happyBirthday(testModel.data(testModel.index(row, 0), "name"))
+ onTapped: testModel.happyBirthday(testModel.data(testModel.index(row, 0), "display"))
}
}
}
diff --git a/tests/auto/qml/qqmltablemodel/data/empty.qml b/tests/auto/qml/qqmltablemodel/data/empty.qml
index 6e66b99145..f5afbd1d27 100644
--- a/tests/auto/qml/qqmltablemodel/data/empty.qml
+++ b/tests/auto/qml/qqmltablemodel/data/empty.qml
@@ -29,8 +29,6 @@
import QtQuick 2.12
import Qt.labs.qmlmodels 1.0
-import "TestUtils.js" as TestUtils
-
Item {
id: root
width: 200
@@ -41,21 +39,37 @@ Item {
function setRows() {
testModel.rows = [
- [
- { name: "John" },
- { age: 22 }
- ],
- [
- { name: "Oliver" },
- { age: 33 }
- ]
+ {
+ name: "John",
+ age: 22
+ },
+ {
+ name: "Oliver",
+ age: 33
+ }
]
}
+ function appendJohn() {
+ testModel.appendRow({
+ name: "John",
+ age: 22
+ })
+ }
+
+ function appendOliver() {
+ testModel.appendRow({
+ name: "Oliver",
+ age: 33
+ })
+ }
+
TableModel {
id: testModel
objectName: "testModel"
- roleDataProvider: TestUtils.testModelRoleDataProvider
+
+ TableModelColumn { display: "name" }
+ TableModelColumn { display: "age" }
}
TableView {
id: tableView
diff --git a/tests/auto/qml/qqmltablemodel/data/explicitDisplayRole.qml b/tests/auto/qml/qqmltablemodel/data/omitTableModelColumnIndex.qml
index 510a62e74b..86bcb08fa2 100644
--- a/tests/auto/qml/qqmltablemodel/data/explicitDisplayRole.qml
+++ b/tests/auto/qml/qqmltablemodel/data/omitTableModelColumnIndex.qml
@@ -28,14 +28,20 @@
import Qt.labs.qmlmodels 1.0
-import "TestUtils.js" as TestUtils
-
TableModel {
- id: testModel
+ objectName: "testModel"
+
+ TableModelColumn { display: "name" }
+ TableModelColumn { display: "age" }
+
rows: [
- [
- { name: "John", display: "foo" },
- { age: 22, display: "bar" }
- ]
+ {
+ name: "John",
+ age: 22
+ },
+ {
+ name: "Oliver",
+ age: 33
+ }
]
}
diff --git a/tests/auto/qml/qqmltablemodel/data/setDataThroughDelegate.qml b/tests/auto/qml/qqmltablemodel/data/setDataThroughDelegate.qml
index 5f849c3350..ebfe4ed930 100644
--- a/tests/auto/qml/qqmltablemodel/data/setDataThroughDelegate.qml
+++ b/tests/auto/qml/qqmltablemodel/data/setDataThroughDelegate.qml
@@ -41,11 +41,11 @@ Item {
signal shouldModifyInvalidType()
function modify() {
- shouldModify();
+ shouldModify()
}
function modifyInvalidRole() {
- shouldModifyInvalidRole();
+ shouldModifyInvalidRole()
}
function modifyInvalidType() {
@@ -54,37 +54,24 @@ Item {
TableView {
anchors.fill: parent
- model: TableModel {
+ model: TestModel {
id: testModel
- objectName: "testModel"
- rows: [
- [
- { name: "John" },
- { age: 22 }
- ],
- [
- { name: "Oliver" },
- { age: 33 }
- ]
- ]
}
delegate: Text {
id: textItem
- // TODO: this is currently random when no roleDataProvider handles it
- // we should allow roleDataProvider to be used to handle specific roles only
text: model.display
Connections {
target: root
enabled: column === 1
- onShouldModify: model.age = 18
+ onShouldModify: model.display = 18
}
Connections {
target: root
enabled: column === 0
- // Invalid: should be "name".
+ // Invalid: should be "display".
onShouldModifyInvalidRole: model.age = 100
}
@@ -92,7 +79,7 @@ Item {
target: root
enabled: column === 1
// Invalid: should be string.
- onShouldModifyInvalidType: model.age = "Whoops"
+ onShouldModifyInvalidType: model.display = "Whoops"
}
}
}
diff --git a/tests/auto/qml/qqmltablemodel/data/setRowsMultipleTimes.qml b/tests/auto/qml/qqmltablemodel/data/setRowsMultipleTimes.qml
index 6aaf79f2d4..01ec40270c 100644
--- a/tests/auto/qml/qqmltablemodel/data/setRowsMultipleTimes.qml
+++ b/tests/auto/qml/qqmltablemodel/data/setRowsMultipleTimes.qml
@@ -39,35 +39,35 @@ Item {
function setRowsValid() {
testModel.rows = [
- [
- { name: "Max" },
- { age: 20 }
- ],
- [
- { name: "Imum" },
- { age: 41 }
- ],
- [
- { name: "Power" },
- { age: 89 }
- ]
+ {
+ name: "Max",
+ age: 20
+ },
+ {
+ name: "Imum",
+ age: 41
+ },
+ {
+ name: "Power",
+ age: 89
+ }
]
}
function setRowsInvalid() {
testModel.rows = [
- [
- { nope: "Nope" },
- { age: 20 }
- ],
- [
- { nope: "Nah" },
- { age: 41 }
- ],
- [
- { nope: "No" },
- { age: 89 }
- ]
+ {
+ nope: "Nope",
+ age: 20
+ },
+ {
+ nope: "Nah",
+ age: 41
+ },
+ {
+ nope: "No",
+ age: 89
+ }
]
}
diff --git a/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp b/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp
index 059ce082d9..113a27494d 100644
--- a/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp
+++ b/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp
@@ -47,6 +47,7 @@ public:
private slots:
void appendRemoveRow();
+ void appendRowToEmptyModel();
void clear();
void getRow();
void insertRow();
@@ -55,15 +56,11 @@ private slots:
void setDataThroughDelegate();
void setRowsImperatively();
void setRowsMultipleTimes();
- void builtInRoles_data();
- void builtInRoles();
- void explicitDisplayRole();
- void roleDataProvider();
void dataAndEditing();
+ void omitTableModelColumnIndex();
+ void complexRow();
};
-static const int builtInRoleCount = 6;
-
void tst_QQmlTableModel::appendRemoveRow()
{
QQuickView view(testFileUrl("common.qml"));
@@ -81,22 +78,15 @@ void tst_QQmlTableModel::appendRemoveRow()
QSignalSpy rowCountSpy(model, SIGNAL(rowCountChanged()));
QVERIFY(rowCountSpy.isValid());
- int heightSignalEmissions = 0;
+ int rowCountSignalEmissions = 0;
const QHash<int, QByteArray> roleNames = model->roleNames();
- QCOMPARE(roleNames.size(), 2 + builtInRoleCount);
- QVERIFY(roleNames.values().contains("name"));
- QVERIFY(roleNames.values().contains("age"));
+ QCOMPARE(roleNames.size(), 1);
QVERIFY(roleNames.values().contains("display"));
- QVERIFY(roleNames.values().contains("decoration"));
- QVERIFY(roleNames.values().contains("edit"));
- QVERIFY(roleNames.values().contains("toolTip"));
- QVERIFY(roleNames.values().contains("statusTip"));
- QVERIFY(roleNames.values().contains("whatsThis"));
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
// Call remove() with a negative rowIndex.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*removeRow\\(\\): \"rowIndex\" cannot be negative"));
@@ -104,7 +94,7 @@ void tst_QQmlTableModel::appendRemoveRow()
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
// Call remove() with an rowIndex that is too large.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
@@ -113,7 +103,7 @@ void tst_QQmlTableModel::appendRemoveRow()
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
// Call remove() with a valid rowIndex but negative rows.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*removeRow\\(\\): \"rows\" is less than or equal to zero"));
@@ -121,7 +111,7 @@ void tst_QQmlTableModel::appendRemoveRow()
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
// Call remove() with a valid rowIndex but excessive rows.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
@@ -130,71 +120,125 @@ void tst_QQmlTableModel::appendRemoveRow()
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
// Call remove() without specifying the number of rows to remove; it should remove one row.
QVERIFY(QMetaObject::invokeMethod(model, "removeRow", Q_ARG(int, 0)));
QCOMPARE(model->rowCount(), 1);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), ++heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), ++rowCountSignalEmissions);
- // Call append() with a row that has a new (and hence unexpected) role.
- QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*appendRow\\(\\): expected 2 columns, but got 3"));
+ // Call append() with a row that has an unexpected role; the row should be added and the extra data ignored.
+ QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "appendRowExtraData"));
+ // Nothing should change.
+ QCOMPARE(model->rowCount(), 2);
+ QCOMPARE(model->columnCount(), 2);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Foo"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 99);
+ QCOMPARE(columnCountSpy.count(), 0);
+ QCOMPARE(rowCountSpy.count(), ++rowCountSignalEmissions);
+
+ // Call append() with a row that is an int.
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
+ ".*appendRow\\(\\): expected \"row\" argument to be a QJSValue, but got int instead:\nQVariant\\(int, 123\\)"));
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "appendRowInvalid1"));
// Nothing should change.
- QCOMPARE(model->rowCount(), 1);
+ QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Foo"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 99);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
- // Call append() with a row that is not an array.
+ // Call append() with a row with a role of the wrong type.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
- ".*appendRow\\(\\): expected \"row\" argument to be an array, but got int instead"));
+ ".*appendRow\\(\\): expected the property named \"age\" to be of type \"int\", but got \"QVariantList\" instead"));
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "appendRowInvalid2"));
// Nothing should change.
- QCOMPARE(model->rowCount(), 1);
+ QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Foo"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 99);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
- // Call append() with a row with a role that is of the wrong type.
+ // Call append() with a row that is an array instead of a simple object.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
- ".*appendRow\\(\\): expected property with type int at column index 1, but got QVariantList instead"));
+ ".*appendRow\\(\\): row manipulation functions do not support complex rows \\(row index: -1\\)"));
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "appendRowInvalid3"));
// Nothing should change.
- QCOMPARE(model->rowCount(), 1);
+ QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
// Call append() to insert one row.
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "appendRow", Q_ARG(QVariant, QLatin1String("Max")), Q_ARG(QVariant, 40)));
- QCOMPARE(model->rowCount(), 2);
+ QCOMPARE(model->rowCount(), 3);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Max"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 40);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Foo"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 99);
+ QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Max"));
+ QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("display")).toInt(), 40);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), ++heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), ++rowCountSignalEmissions);
// Call remove() and specify rowIndex and rows, removing all remaining rows.
- QVERIFY(QMetaObject::invokeMethod(model, "removeRow", Q_ARG(int, 0), Q_ARG(int, 2)));
+ QVERIFY(QMetaObject::invokeMethod(model, "removeRow", Q_ARG(int, 0), Q_ARG(int, 3)));
QCOMPARE(model->rowCount(), 0);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")), QVariant());
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")), QVariant());
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")), QVariant());
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")), QVariant());
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), ++heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), ++rowCountSignalEmissions);
+}
+
+void tst_QQmlTableModel::appendRowToEmptyModel()
+{
+ QQuickView view(testFileUrl("empty.qml"));
+ QCOMPARE(view.status(), QQuickView::Ready);
+ view.show();
+ QVERIFY(QTest::qWaitForWindowActive(&view));
+
+ QQmlTableModel *model = view.rootObject()->property("testModel").value<QQmlTableModel*>();
+ QVERIFY(model);
+ QCOMPARE(model->rowCount(), 0);
+ QCOMPARE(model->columnCount(), 2);
+
+ QSignalSpy columnCountSpy(model, SIGNAL(columnCountChanged()));
+ QVERIFY(columnCountSpy.isValid());
+
+ QSignalSpy rowCountSpy(model, SIGNAL(rowCountChanged()));
+ QVERIFY(rowCountSpy.isValid());
+
+ QQuickTableView *tableView = view.rootObject()->property("tableView").value<QQuickTableView*>();
+ QVERIFY(tableView);
+ QCOMPARE(tableView->rows(), 0);
+ QCOMPARE(tableView->columns(), 2);
+
+ QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "appendJohn"));
+ QCOMPARE(model->rowCount(), 1);
+ QCOMPARE(model->columnCount(), 2);
+ const QHash<int, QByteArray> roleNames = model->roleNames();
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(columnCountSpy.count(), 0);
+ QCOMPARE(rowCountSpy.count(), 1);
+ QTRY_COMPARE(tableView->rows(), 1);
+ QCOMPARE(tableView->columns(), 2);
}
void tst_QQmlTableModel::clear()
@@ -216,9 +260,8 @@ void tst_QQmlTableModel::clear()
QVERIFY(rowCountSpy.isValid());
const QHash<int, QByteArray> roleNames = model->roleNames();
- QVERIFY(roleNames.values().contains("name"));
- QVERIFY(roleNames.values().contains("age"));
- QCOMPARE(roleNames.size(), 2 + builtInRoleCount);
+ QVERIFY(roleNames.values().contains("display"));
+ QCOMPARE(roleNames.size(), 1);
QQuickTableView *tableView = view.rootObject()->property("tableView").value<QQuickTableView*>();
QVERIFY(tableView);
@@ -263,9 +306,9 @@ void tst_QQmlTableModel::getRow()
// Call get() with a valid row index.
QVERIFY(QMetaObject::invokeMethod(model, "getRow", Q_RETURN_ARG(QVariant, returnValue), Q_ARG(int, 0)));
- const QVariantList rowAsVariantList = returnValue.toList();
- QCOMPARE(rowAsVariantList.at(0).toMap().value(QLatin1String("name")), QLatin1String("John"));
- QCOMPARE(rowAsVariantList.at(1).toMap().value(QLatin1String("age")), 22);
+ const QVariantMap rowAsVariantMap = returnValue.toMap();
+ QCOMPARE(rowAsVariantMap.value(QLatin1String("name")).toString(), QLatin1String("John"));
+ QCOMPARE(rowAsVariantMap.value(QLatin1String("age")).toInt(), 22);
}
void tst_QQmlTableModel::insertRow()
@@ -285,7 +328,7 @@ void tst_QQmlTableModel::insertRow()
QSignalSpy rowCountSpy(model, SIGNAL(rowCountChanged()));
QVERIFY(rowCountSpy.isValid());
- int heightSignalEmissions = 0;
+ int rowCountSignalEmissions = 0;
QQuickTableView *tableView = view.rootObject()->property("tableView").value<QQuickTableView*>();
QVERIFY(tableView);
@@ -300,12 +343,12 @@ void tst_QQmlTableModel::insertRow()
QCOMPARE(model->columnCount(), 2);
QCOMPARE(model->rowCount(), 2);
const QHash<int, QByteArray> roleNames = model->roleNames();
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
QCOMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
@@ -316,92 +359,112 @@ void tst_QQmlTableModel::insertRow()
Q_ARG(QVariant, QLatin1String("Max")), Q_ARG(QVariant, 40), Q_ARG(QVariant, 3)));
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
QCOMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
- // Try to insert a row that has a new (and hence unexpected) role.
- QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*insertRow\\(\\): expected 2 columns, but got 3"));
+ // Call insert() with a row that is an int.
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
+ ".*insertRow\\(\\): expected \"row\" argument to be a QJSValue, but got int instead:\nQVariant\\(int, 123\\)"));
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "insertRowInvalid1"));
- QCOMPARE(model->columnCount(), 2);
QCOMPARE(model->rowCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->columnCount(), 2);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
QCOMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
- // Try to insert a row that is not an array.
+ // Try to insert a row with a role of the wrong type.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
- ".*insertRow\\(\\): expected \"row\" argument to be an array, but got int instead"));
+ ".*insertRow\\(\\): expected the property named \"age\" to be of type \"int\", but got \"QVariantList\" instead"));
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "insertRowInvalid2"));
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
QCOMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
- // Try to insert a row with a role that is of the wrong type.
+ // Try to insert a row that is an array instead of a simple object.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
- ".*insertRow\\(\\): expected property with type int at column index 1, but got QVariantList instead"));
+ ".*insertRow\\(\\): row manipulation functions do not support complex rows \\(row index: 0\\)"));
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "insertRowInvalid3"));
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
QCOMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
- // Insert a row at the bottom of the table.
- QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "insertRow",
- Q_ARG(QVariant, QLatin1String("Max")), Q_ARG(QVariant, 40), Q_ARG(QVariant, 2)));
+ // Try to insert a row has an unexpected role; the row should be added and the extra data ignored.
+ QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "insertRowExtraData"));
QCOMPARE(model->rowCount(), 3);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
- QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Max"));
- QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("age")).toInt(), 40);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Foo"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 99);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), ++heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), ++rowCountSignalEmissions);
QTRY_COMPARE(tableView->rows(), 3);
QCOMPARE(tableView->columns(), 2);
- // Insert a row in the middle of the table.
+ // Insert a row at the bottom of the table.
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "insertRow",
- Q_ARG(QVariant, QLatin1String("Daisy")), Q_ARG(QVariant, 30), Q_ARG(QVariant, 1)));
+ Q_ARG(QVariant, QLatin1String("Max")), Q_ARG(QVariant, 40), Q_ARG(QVariant, 3)));
QCOMPARE(model->rowCount(), 4);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Daisy"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 30);
- QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
- QCOMPARE(model->data(model->index(3, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Max"));
- QCOMPARE(model->data(model->index(3, 1, QModelIndex()), roleNames.key("age")).toInt(), 40);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Foo"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 99);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QCOMPARE(model->data(model->index(3, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Max"));
+ QCOMPARE(model->data(model->index(3, 1, QModelIndex()), roleNames.key("display")).toInt(), 40);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), ++heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), ++rowCountSignalEmissions);
QTRY_COMPARE(tableView->rows(), 4);
QCOMPARE(tableView->columns(), 2);
+
+ // Insert a row in the middle of the table.
+ QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "insertRow",
+ Q_ARG(QVariant, QLatin1String("Daisy")), Q_ARG(QVariant, 30), Q_ARG(QVariant, 2)));
+ QCOMPARE(model->rowCount(), 5);
+ QCOMPARE(model->columnCount(), 2);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Foo"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 99);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Daisy"));
+ QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("display")).toInt(), 30);
+ QCOMPARE(model->data(model->index(3, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(3, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Max"));
+ QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("display")).toInt(), 40);
+ QCOMPARE(columnCountSpy.count(), 0);
+ QCOMPARE(rowCountSpy.count(), ++rowCountSignalEmissions);
+ QTRY_COMPARE(tableView->rows(), 5);
+ QCOMPARE(tableView->columns(), 2);
}
void tst_QQmlTableModel::moveRow()
@@ -421,7 +484,7 @@ void tst_QQmlTableModel::moveRow()
QSignalSpy rowCountSpy(model, SIGNAL(rowCountChanged()));
QVERIFY(rowCountSpy.isValid());
- int heightSignalEmissions = 0;
+ int rowCountSignalEmissions = 0;
const QHash<int, QByteArray> roleNames = model->roleNames();
@@ -431,105 +494,105 @@ void tst_QQmlTableModel::moveRow()
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "appendRow", Q_ARG(QVariant, QLatin1String("Trev")), Q_ARG(QVariant, 48)));
QCOMPARE(model->rowCount(), 5);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
- QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Max"));
- QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("age")).toInt(), 40);
- QCOMPARE(model->data(model->index(3, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Daisy"));
- QCOMPARE(model->data(model->index(3, 1, QModelIndex()), roleNames.key("age")).toInt(), 30);
- QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Trev"));
- QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("age")).toInt(), 48);
- heightSignalEmissions = 3;
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Max"));
+ QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("display")).toInt(), 40);
+ QCOMPARE(model->data(model->index(3, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Daisy"));
+ QCOMPARE(model->data(model->index(3, 1, QModelIndex()), roleNames.key("display")).toInt(), 30);
+ QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Trev"));
+ QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("display")).toInt(), 48);
+ rowCountSignalEmissions = 3;
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
// Try to move with a fromRowIndex that is negative.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*moveRow\\(\\): \"fromRowIndex\" cannot be negative"));
QVERIFY(QMetaObject::invokeMethod(model, "moveRow", Q_ARG(int, -1), Q_ARG(int, 1)));
// Shouldn't have changed.
- QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Trev"));
- QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("age")).toInt(), 48);
+ QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Trev"));
+ QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("display")).toInt(), 48);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
// Try to move with a fromRowIndex that is too large.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*moveRow\\(\\): \"fromRowIndex\" 5 is greater than or equal to rowCount\\(\\)"));
QVERIFY(QMetaObject::invokeMethod(model, "moveRow", Q_ARG(int, 5), Q_ARG(int, 1)));
- QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Trev"));
- QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("age")).toInt(), 48);
+ QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Trev"));
+ QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("display")).toInt(), 48);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
// Try to move with a toRowIndex that is negative.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*moveRow\\(\\): \"toRowIndex\" cannot be negative"));
QVERIFY(QMetaObject::invokeMethod(model, "moveRow", Q_ARG(int, 0), Q_ARG(int, -1)));
// Shouldn't have changed.
- QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Trev"));
- QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("age")).toInt(), 48);
+ QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Trev"));
+ QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("display")).toInt(), 48);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
// Try to move with a toRowIndex that is too large.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*moveRow\\(\\): \"toRowIndex\" 5 is greater than or equal to rowCount\\(\\)"));
QVERIFY(QMetaObject::invokeMethod(model, "moveRow", Q_ARG(int, 0), Q_ARG(int, 5)));
- QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Trev"));
- QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("age")).toInt(), 48);
+ QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Trev"));
+ QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("display")).toInt(), 48);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
// Move the first row to the end.
QVERIFY(QMetaObject::invokeMethod(model, "moveRow", Q_ARG(int, 0), Q_ARG(int, 4)));
// The counts shouldn't have changed.
QCOMPARE(model->rowCount(), 5);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Max"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 40);
- QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Daisy"));
- QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("age")).toInt(), 30);
- QCOMPARE(model->data(model->index(3, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Trev"));
- QCOMPARE(model->data(model->index(3, 1, QModelIndex()), roleNames.key("age")).toInt(), 48);
- QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Max"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 40);
+ QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Daisy"));
+ QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("display")).toInt(), 30);
+ QCOMPARE(model->data(model->index(3, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Trev"));
+ QCOMPARE(model->data(model->index(3, 1, QModelIndex()), roleNames.key("display")).toInt(), 48);
+ QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
// Move it back again.
QVERIFY(QMetaObject::invokeMethod(model, "moveRow", Q_ARG(int, 4), Q_ARG(int, 0)));
QCOMPARE(model->rowCount(), 5);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
- QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Max"));
- QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("age")).toInt(), 40);
- QCOMPARE(model->data(model->index(3, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Daisy"));
- QCOMPARE(model->data(model->index(3, 1, QModelIndex()), roleNames.key("age")).toInt(), 30);
- QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Trev"));
- QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("age")).toInt(), 48);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Max"));
+ QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("display")).toInt(), 40);
+ QCOMPARE(model->data(model->index(3, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Daisy"));
+ QCOMPARE(model->data(model->index(3, 1, QModelIndex()), roleNames.key("display")).toInt(), 30);
+ QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Trev"));
+ QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("display")).toInt(), 48);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
// Move the first row down one by one row.
QVERIFY(QMetaObject::invokeMethod(model, "moveRow", Q_ARG(int, 0), Q_ARG(int, 1)));
QCOMPARE(model->rowCount(), 5);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Max"));
- QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("age")).toInt(), 40);
- QCOMPARE(model->data(model->index(3, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Daisy"));
- QCOMPARE(model->data(model->index(3, 1, QModelIndex()), roleNames.key("age")).toInt(), 30);
- QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Trev"));
- QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("age")).toInt(), 48);
- QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Max"));
+ QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("display")).toInt(), 40);
+ QCOMPARE(model->data(model->index(3, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Daisy"));
+ QCOMPARE(model->data(model->index(3, 1, QModelIndex()), roleNames.key("display")).toInt(), 30);
+ QCOMPARE(model->data(model->index(4, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Trev"));
+ QCOMPARE(model->data(model->index(4, 1, QModelIndex()), roleNames.key("display")).toInt(), 48);
+ QCOMPARE(columnCountSpy.count(), 0);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
}
void tst_QQmlTableModel::setRow()
@@ -549,14 +612,14 @@ void tst_QQmlTableModel::setRow()
QSignalSpy rowCountSpy(model, SIGNAL(rowCountChanged()));
QVERIFY(rowCountSpy.isValid());
- int heightSignalEmissions = 0;
+ int rowCountSignalEmissions = 0;
QQuickTableView *tableView = view.rootObject()->property("tableView").value<QQuickTableView*>();
QVERIFY(tableView);
QCOMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
- // Try to insert with a negative index.
+ // Try to set with a negative index.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
".*setRow\\(\\): \"rowIndex\" cannot be negative"));
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "setRow",
@@ -564,72 +627,86 @@ void tst_QQmlTableModel::setRow()
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
const QHash<int, QByteArray> roleNames = model->roleNames();
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
QCOMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
- // Try to insert past the last allowed index.
+ // Try to set at an index past the last allowed index.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
".*setRow\\(\\): \"rowIndex\" 3 is greater than rowCount\\(\\) of 2"));
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "setRow",
Q_ARG(QVariant, 3), Q_ARG(QVariant, QLatin1String("Max")), Q_ARG(QVariant, 40)));
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
QCOMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
- // Try to insert a row that has a new (and hence unexpected) role.
- QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*setRow\\(\\): expected 2 columns, but got 3"));
- QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "setRowInvalid1"));
+ // Try to set a row that has an unexpected role; the row should be set and the extra data ignored.
+ QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "setRowExtraData"));
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Foo"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 99);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
QCOMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
// Try to insert a row that is not an array.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
- ".*setRow\\(\\): expected \"row\" argument to be an array, but got int instead"));
- QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "setRowInvalid2"));
+ ".*setRow\\(\\): expected \"row\" argument to be a QJSValue, but got int instead:\nQVariant\\(int, 123\\)"));
+ QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "setRowInvalid1"));
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Foo"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 99);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
QCOMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
// Try to insert a row with a role that is of the wrong type.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
- ".*setRow\\(\\): expected property with type int at column index 1, but got QVariantList instead"));
+ ".*setRow\\(\\): expected the property named \"age\" to be of type \"int\", but got \"QVariantList\" instead"));
+ QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "setRowInvalid2"));
+ QCOMPARE(model->rowCount(), 2);
+ QCOMPARE(model->columnCount(), 2);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Foo"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 99);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QCOMPARE(columnCountSpy.count(), 0);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
+ QCOMPARE(tableView->rows(), 2);
+ QCOMPARE(tableView->columns(), 2);
+
+ // Try to insert a row that is an array instead of a simple object.
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
+ ".*setRow\\(\\): row manipulation functions do not support complex rows \\(row index: 0\\)"));
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "setRowInvalid3"));
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Foo"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 99);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
QCOMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
@@ -638,12 +715,12 @@ void tst_QQmlTableModel::setRow()
Q_ARG(QVariant, 0), Q_ARG(QVariant, QLatin1String("Max")), Q_ARG(QVariant, 40)));
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Max"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 40);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Max"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 40);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
QCOMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
@@ -652,12 +729,12 @@ void tst_QQmlTableModel::setRow()
Q_ARG(QVariant, 1), Q_ARG(QVariant, QLatin1String("Daisy")), Q_ARG(QVariant, 30)));
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Max"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 40);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Daisy"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 30);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Max"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 40);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Daisy"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 30);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), rowCountSignalEmissions);
QCOMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
@@ -666,14 +743,14 @@ void tst_QQmlTableModel::setRow()
Q_ARG(QVariant, 2), Q_ARG(QVariant, QLatin1String("Wot")), Q_ARG(QVariant, 99)));
QCOMPARE(model->rowCount(), 3);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Max"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 40);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Daisy"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 30);
- QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Wot"));
- QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("age")).toInt(), 99);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Max"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 40);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Daisy"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 30);
+ QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Wot"));
+ QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("display")).toInt(), 99);
QCOMPARE(columnCountSpy.count(), 0);
- QCOMPARE(rowCountSpy.count(), ++heightSignalEmissions);
+ QCOMPARE(rowCountSpy.count(), ++rowCountSignalEmissions);
QTRY_COMPARE(tableView->rows(), 3);
QCOMPARE(tableView->columns(), 2);
}
@@ -697,56 +774,50 @@ void tst_QQmlTableModel::setDataThroughDelegate()
QVERIFY(rowCountSpy.isValid());
const QHash<int, QByteArray> roleNames = model->roleNames();
- QCOMPARE(roleNames.size(), 2 + builtInRoleCount);
- QVERIFY(roleNames.values().contains("name"));
- QVERIFY(roleNames.values().contains("age"));
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
+ QCOMPARE(roleNames.size(), 1);
+ QVERIFY(roleNames.values().contains("display"));
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
QCOMPARE(columnCountSpy.count(), 0);
QCOMPARE(rowCountSpy.count(), 0);
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "modify"));
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 18);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 18);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 18);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 18);
QCOMPARE(columnCountSpy.count(), 0);
QCOMPARE(rowCountSpy.count(), 0);
// Test setting a role that doesn't exist for a certain column.
- const auto invalidRoleRegEx = QRegularExpression(".*setData\\(\\): no role named \"age\" at column index 0. " \
- "The available roles for that column are:[\r\n] - \"name\" \\(QString\\)");
- // There are two rows, so two delegates respond to the signal, which means we need to ignore two warnings.
- QTest::ignoreMessage(QtWarningMsg, invalidRoleRegEx);
- QTest::ignoreMessage(QtWarningMsg, invalidRoleRegEx);
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "modifyInvalidRole"));
// Should be unchanged.
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 18);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 18);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 18);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 18);
QCOMPARE(columnCountSpy.count(), 0);
QCOMPARE(rowCountSpy.count(), 0);
// Test setting a role with a value of the wrong type.
// There are two rows, so two delegates respond to the signal, which means we need to ignore two warnings.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*setData\\(\\): failed converting value QVariant\\(QString, \"Whoops\"\\) " \
- "set at row 0 column 1 with role \"age\" to \"int\""));
+ "set at row 0 column 1 with role \"display\" to \"int\""));
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*setData\\(\\): failed converting value QVariant\\(QString, \"Whoops\"\\) " \
- "set at row 1 column 1 with role \"age\" to \"int\""));
+ "set at row 1 column 1 with role \"display\" to \"int\""));
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "modifyInvalidType"));
// Should be unchanged.
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 18);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 18);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 18);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 18);
QCOMPARE(columnCountSpy.count(), 0);
QCOMPARE(rowCountSpy.count(), 0);
}
-// Start off with empty rows and append to test widthChanged().
+// Start off with empty rows and then set them to test rowCountChanged().
void tst_QQmlTableModel::setRowsImperatively()
{
QQuickView view(testFileUrl("empty.qml"));
@@ -756,8 +827,8 @@ void tst_QQmlTableModel::setRowsImperatively()
QQmlTableModel *model = view.rootObject()->property("testModel").value<QQmlTableModel*>();
QVERIFY(model);
- QCOMPARE(model->columnCount(), 0);
QCOMPARE(model->rowCount(), 0);
+ QCOMPARE(model->columnCount(), 2);
QSignalSpy columnCountSpy(model, SIGNAL(columnCountChanged()));
QVERIFY(columnCountSpy.isValid());
@@ -768,17 +839,17 @@ void tst_QQmlTableModel::setRowsImperatively()
QQuickTableView *tableView = view.rootObject()->property("tableView").value<QQuickTableView*>();
QVERIFY(tableView);
QCOMPARE(tableView->rows(), 0);
- QCOMPARE(tableView->columns(), 0);
+ QCOMPARE(tableView->columns(), 2);
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "setRows"));
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
const QHash<int, QByteArray> roleNames = model->roleNames();
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 33);
- QCOMPARE(columnCountSpy.count(), 1);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QCOMPARE(columnCountSpy.count(), 0);
QCOMPARE(rowCountSpy.count(), 1);
QTRY_COMPARE(tableView->rows(), 2);
QCOMPARE(tableView->columns(), 2);
@@ -812,134 +883,96 @@ void tst_QQmlTableModel::setRowsMultipleTimes()
QCOMPARE(model->rowCount(), 3);
QCOMPARE(model->columnCount(), 2);
const QHash<int, QByteArray> roleNames = model->roleNames();
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Max"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 20);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Imum"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 41);
- QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Power"));
- QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("age")).toInt(), 89);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Max"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 20);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Imum"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 41);
+ QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Power"));
+ QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("display")).toInt(), 89);
QCOMPARE(columnCountSpy.count(), 0);
QCOMPARE(rowCountSpy.count(), 1);
QTRY_COMPARE(tableView->rows(), 3);
QCOMPARE(tableView->columns(), 2);
// Set invalid rows; we should get a warning and nothing should change.
- // TODO: add quotes to the warning message
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
- ".*setRows\\(\\): expected property named name at column index 0, but got nope instead"));
+ ".*setRows\\(\\): expected a property named \"name\" in row at index 0, but couldn't find one"));
QVERIFY(QMetaObject::invokeMethod(view.rootObject(), "setRowsInvalid"));
QCOMPARE(model->rowCount(), 3);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Max"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 20);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Imum"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("age")).toInt(), 41);
- QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Power"));
- QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("age")).toInt(), 89);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Max"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 20);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Imum"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 41);
+ QCOMPARE(model->data(model->index(2, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Power"));
+ QCOMPARE(model->data(model->index(2, 1, QModelIndex()), roleNames.key("display")).toInt(), 89);
QCOMPARE(columnCountSpy.count(), 0);
QCOMPARE(rowCountSpy.count(), 1);
QCOMPARE(tableView->rows(), 3);
QCOMPARE(tableView->columns(), 2);
}
-void tst_QQmlTableModel::builtInRoles_data()
-{
- QTest::addColumn<int>("row");
- QTest::addColumn<int>("column");
- QTest::addColumn<QByteArray>("roleName");
- QTest::addColumn<QVariant>("expectedValue");
-
- const QByteArray displayRole = "display";
-
- QTest::addRow("display(0,0)") << 0 << 0 << displayRole << QVariant(QLatin1String("John"));
- QTest::addRow("display(0,1)") << 0 << 1 << displayRole << QVariant(QLatin1String("22"));
- QTest::addRow("display(1,0)") << 1 << 0 << displayRole << QVariant(QLatin1String("Oliver"));
- QTest::addRow("display(1,1)") << 1 << 1 << displayRole << QVariant(QLatin1String("33"));
-}
-
-void tst_QQmlTableModel::builtInRoles()
+void tst_QQmlTableModel::dataAndEditing()
{
- QFETCH(int, row);
- QFETCH(int, column);
- QFETCH(QByteArray, roleName);
- QFETCH(QVariant, expectedValue);
-
- QQmlEngine engine;
- QQmlComponent component(&engine, testFileUrl("builtInRoles.qml"));
- QCOMPARE(component.status(), QQmlComponent::Ready);
+ QQuickView view(testFileUrl("dataAndSetData.qml"));
+ QCOMPARE(view.status(), QQuickView::Ready);
+ view.show();
+ QVERIFY(QTest::qWaitForWindowActive(&view));
- QScopedPointer<QQmlTableModel> model(qobject_cast<QQmlTableModel*>(component.create()));
+ QQmlTableModel *model = view.rootObject()->property("model").value<QQmlTableModel*>();
QVERIFY(model);
- QCOMPARE(model->rowCount(), 2);
- QCOMPARE(model->columnCount(), 2);
const QHash<int, QByteArray> roleNames = model->roleNames();
- QCOMPARE(roleNames.size(), 4 + builtInRoleCount);
QVERIFY(roleNames.values().contains("display"));
- QVERIFY(roleNames.values().contains("decoration"));
- QVERIFY(roleNames.values().contains("edit"));
- QVERIFY(roleNames.values().contains("toolTip"));
- QVERIFY(roleNames.values().contains("statusTip"));
- QVERIFY(roleNames.values().contains("whatsThis"));
- QVERIFY(roleNames.values().contains("name"));
- QVERIFY(roleNames.values().contains("age"));
- QVERIFY(roleNames.values().contains("someOtherRole1"));
- QVERIFY(roleNames.values().contains("someOtherRole2"));
- QCOMPARE(model->data(model->index(row, column, QModelIndex()), roleNames.key(roleName)), expectedValue);
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QVERIFY(QMetaObject::invokeMethod(model, "happyBirthday", Q_ARG(QVariant, QLatin1String("Oliver"))));
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 34);
}
-void tst_QQmlTableModel::explicitDisplayRole()
+void tst_QQmlTableModel::omitTableModelColumnIndex()
{
QQmlEngine engine;
- QQmlComponent component(&engine, testFileUrl("explicitDisplayRole.qml"));
+ QQmlComponent component(&engine, testFileUrl("omitTableModelColumnIndex.qml"));
QCOMPARE(component.status(), QQmlComponent::Ready);
QScopedPointer<QQmlTableModel> model(qobject_cast<QQmlTableModel*>(component.create()));
QVERIFY(model);
- QCOMPARE(model->rowCount(), 1);
+ QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- const QHash<int, QByteArray> roleNames = model->roleNames();
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("foo"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("bar"));
-}
-
-void tst_QQmlTableModel::roleDataProvider()
-{
- QQuickView view(testFileUrl("roleDataProvider.qml"));
- QCOMPARE(view.status(), QQuickView::Ready);
- view.show();
- QVERIFY(QTest::qWaitForWindowActive(&view));
-
- QQmlTableModel *model = view.rootObject()->property("testModel").value<QQmlTableModel*>();
- QVERIFY(model);
const QHash<int, QByteArray> roleNames = model->roleNames();
- QVERIFY(roleNames.values().contains("display"));
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Rex"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 3 * 7);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Buster"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 5 * 7);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
}
-void tst_QQmlTableModel::dataAndEditing()
+void tst_QQmlTableModel::complexRow()
{
- QQuickView view(testFileUrl("dataAndSetData.qml"));
+ QQuickView view(testFileUrl("complex.qml"));
QCOMPARE(view.status(), QQuickView::Ready);
view.show();
QVERIFY(QTest::qWaitForWindowActive(&view));
- QQmlTableModel *model = view.rootObject()->property("model").value<QQmlTableModel*>();
+ QQuickTableView *tableView = qobject_cast<QQuickTableView*>(view.rootObject());
+ QVERIFY(tableView);
+ QCOMPARE(tableView->rows(), 2);
+ QCOMPARE(tableView->columns(), 2);
+
+ QQmlTableModel *model = tableView->model().value<QQmlTableModel*>();
QVERIFY(model);
+ QCOMPARE(model->rowCount(), 2);
+ QCOMPARE(model->columnCount(), 2);
const QHash<int, QByteArray> roleNames = model->roleNames();
- QVERIFY(roleNames.values().contains("display"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
- QVERIFY(QMetaObject::invokeMethod(model, "happyBirthday", Q_ARG(QVariant, QLatin1String("Oliver"))));
QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 34);
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
}
QTEST_MAIN(tst_QQmlTableModel)
diff --git a/tests/manual/tableview/tablemodel/form/RowForm.qml b/tests/manual/tableview/tablemodel/form/RowForm.qml
index 428682008a..bb03e685c0 100644
--- a/tests/manual/tableview/tablemodel/form/RowForm.qml
+++ b/tests/manual/tableview/tablemodel/form/RowForm.qml
@@ -45,13 +45,13 @@ ScrollView {
clip: true
function inputAsRow() {
- return [
- { checkable: checkableCheckBox.checked, checked: checkedCheckBox.checked },
- { amount: amountSpinBox.value },
- { fruitType: fruitTypeTextField.text },
- { fruitName: fruitNameTextField.text },
- { fruitPrice: parseFloat(fruitPriceTextField.text) },
- ]
+ return {
+ checked: checkedCheckBox.checked,
+ amount: amountSpinBox.value,
+ fruitType: fruitTypeTextField.text,
+ fruitName: fruitNameTextField.text,
+ fruitPrice: parseFloat(fruitPriceTextField.text)
+ }
}
default property alias content: gridLayout.children
@@ -60,23 +60,11 @@ ScrollView {
id: gridLayout
columns: 2
- RowLayout {
- Layout.columnSpan: 2
-
- Label {
- text: "checkable"
- }
- CheckBox {
- id: checkableCheckBox
- checked: true
- }
-
- Label {
- text: "checked"
- }
- CheckBox {
- id: checkedCheckBox
- }
+ Label {
+ text: "checked"
+ }
+ CheckBox {
+ id: checkedCheckBox
}
Label {
diff --git a/tests/manual/tableview/tablemodel/form/main.qml b/tests/manual/tableview/tablemodel/form/main.qml
index 21ecd8edbb..6c6874fb4b 100644
--- a/tests/manual/tableview/tablemodel/form/main.qml
+++ b/tests/manual/tableview/tablemodel/form/main.qml
@@ -64,31 +64,37 @@ ApplicationWindow {
Layout.fillHeight: true
model: TableModel {
+ TableModelColumn { display: "checked" }
+ TableModelColumn { display: "amount" }
+ TableModelColumn { display: "fruitType" }
+ TableModelColumn { display: "fruitName" }
+ TableModelColumn { display: "fruitPrice" }
+
// One row = one type of fruit that can be ordered
rows: [
- [
- // Each object (line) is one cell/column,
- // and each property in that object is a role.
- { checked: false, checkable: true },
- { amount: 1 },
- { fruitType: "Apple" },
- { fruitName: "Granny Smith" },
- { fruitPrice: 1.50 }
- ],
- [
- { checked: true, checkable: true },
- { amount: 4 },
- { fruitType: "Orange" },
- { fruitName: "Navel" },
- { fruitPrice: 2.50 }
- ],
- [
- { checked: false, checkable: true },
- { amount: 1 },
- { fruitType: "Banana" },
- { fruitName: "Cavendish" },
- { fruitPrice: 3.50 }
- ]
+ {
+ // Each object (line) is one column,
+ // and each property in that object represents a role.
+ checked: false,
+ amount: 1,
+ fruitType: "Apple",
+ fruitName: "Granny Smith",
+ fruitPrice: 1.50
+ },
+ {
+ checked: true,
+ amount: 4,
+ fruitType: "Orange",
+ fruitName: "Navel",
+ fruitPrice: 2.50
+ },
+ {
+ checked: false,
+ amount: 1,
+ fruitType: "Banana",
+ fruitName: "Cavendish",
+ fruitPrice: 3.50
+ }
]
}
@@ -97,16 +103,16 @@ ApplicationWindow {
column: 0
delegate: CheckBox {
objectName: "tableViewCheckBoxDelegate"
- checked: model.checked
- onToggled: model.checked = checked
+ checked: model.display
+ onToggled: model.display = display
}
}
DelegateChoice {
column: 1
delegate: SpinBox {
objectName: "tableViewSpinBoxDelegate"
- value: model.amount
- onValueModified: model.amount = value
+ value: model.display
+ onValueModified: model.display = value
}
}
DelegateChoice {