diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2018-08-06 13:49:05 +0200 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2018-08-15 08:48:22 +0000 |
commit | 9e73c8f3ac82272107b641ed7ec9223fcd1b84b1 (patch) | |
tree | 91d496ac59d405c02b1ba568223f6d71bd891918 /tests/manual | |
parent | bdeaef727230b6e820b202828e06503dac8096d6 (diff) |
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 <mitch.curtis@qt.io>
Diffstat (limited to 'tests/manual')
-rw-r--r-- | tests/manual/tableview/abstracttablemodel/main.cpp | 48 | ||||
-rw-r--r-- | tests/manual/tableview/abstracttablemodel/main.qml | 17 |
2 files changed, 54 insertions, 11 deletions
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 <QGuiApplication> #include <QQmlApplicationEngine> #include <QAbstractTableModel> +#include <QSet> 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<int, QByteArray> 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<int> 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 + } + } } } } |