From 9e73c8f3ac82272107b641ed7ec9223fcd1b84b1 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Mon, 6 Aug 2018 13:49:05 +0200 Subject: test, abstractitemmodel: improve manual test Improve the test so that you can click on the cells to change their color. Change-Id: Ia74620894f2885242f587c4c863bcf3544b13488 Reviewed-by: Mitch Curtis --- tests/manual/tableview/abstracttablemodel/main.cpp | 48 ++++++++++++++++++---- tests/manual/tableview/abstracttablemodel/main.qml | 17 ++++++-- 2 files changed, 54 insertions(+), 11 deletions(-) (limited to 'tests/manual') diff --git a/tests/manual/tableview/abstracttablemodel/main.cpp b/tests/manual/tableview/abstracttablemodel/main.cpp index 22dda5ca4a..6e300184be 100644 --- a/tests/manual/tableview/abstracttablemodel/main.cpp +++ b/tests/manual/tableview/abstracttablemodel/main.cpp @@ -40,6 +40,7 @@ #include #include #include +#include class TestTableModel : public QAbstractTableModel { @@ -56,23 +57,54 @@ public: int columnCount(const QModelIndex & = QModelIndex()) const override { return m_cols; } void setColumnCount(int count) { beginResetModel(); m_cols = count; emit columnCountChanged(); endResetModel(); } - QVariant headerData(int section, Qt::Orientation orientation, int role) const + int indexValue(const QModelIndex &index) const { - Q_UNUSED(orientation); - Q_UNUSED(role); - return QStringLiteral("Column header"); + return index.row() + (index.column() * rowCount()); } QVariant data(const QModelIndex &index, int role) const override { - if (!index.isValid() || role != Qt::DisplayRole) + if (!index.isValid()) return QVariant(); - return QString("[%1-%2]").arg(index.column()).arg(index.row()); + + switch (role) { + case Qt::DisplayRole: + return QString("%1, %2").arg(index.column()).arg(index.row()); + case Qt::CheckStateRole: + return m_checkedCells.contains(indexValue(index)); + default: + return QVariant(); + } + + return QVariant(); + } + + bool setData(const QModelIndex &index, const QVariant &value, + int role = Qt::EditRole) override + { + if (role != Qt::CheckStateRole) + return false; + + int i = indexValue(index); + bool checked = value.toBool(); + if (checked == m_checkedCells.contains(i)) + return false; + + if (checked) + m_checkedCells.insert(i); + else + m_checkedCells.remove(i); + + emit dataChanged(index, index, {role}); + return true; } QHash roleNames() const override { - return { {Qt::DisplayRole, "display"} }; + return { + {Qt::DisplayRole, "display"}, + {Qt::CheckStateRole, "checked"} + }; } signals: @@ -82,6 +114,8 @@ signals: private: int m_rows = 0; int m_cols = 0; + + QSet m_checkedCells; }; int main(int argc, char *argv[]) diff --git a/tests/manual/tableview/abstracttablemodel/main.qml b/tests/manual/tableview/abstracttablemodel/main.qml index a9b8da94c6..6e694c71a2 100644 --- a/tests/manual/tableview/abstracttablemodel/main.qml +++ b/tests/manual/tableview/abstracttablemodel/main.qml @@ -75,12 +75,21 @@ Window { Component { id: tableViewDelegate Rectangle { - implicitWidth: column % 3 ? 80 : 50 - implicitHeight: row % 3 ? 80 : 50 + id: delegate + implicitWidth: 100 + implicitHeight: 50 + color: checked ? "lightblue" : "white" Text { - anchors.centerIn: parent - text: modelData + anchors.fill: parent + text: display + + MouseArea { + anchors.fill: parent + onClicked: { + checked = !checked + } + } } } } -- cgit v1.2.3