aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/types
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2018-12-18 23:48:11 +0100
committerShawn Rutledge <shawn.rutledge@qt.io>2019-02-22 17:32:10 +0000
commitb15654d77f7886ae23f06d760b90973983e12779 (patch)
tree8e21d6e4022c77e41180190ccd66c37b57db4cf1 /src/qml/types
parent22435f49499342ff102411c025efb8007dfa62a2 (diff)
TableModel: add QML-invokable overloads for index(), data() and setData()
data() needs to be invokable because it does more processing to ensure that DisplayRole will give us something, and we want to be able to access it in JS code too, not only via the role context property in a delegate binding. index() needs to be invokable to use it when calling data(). It's useful for setData() to be invokable so that TableView delegates can be used to edit the model. However since we don't normally expose numeric roles to QML, we use string roles, and have to look them up in the roleNames() hash. Change-Id: I38904ac995fc2bac514bde2dd37a95e0b911c00c Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io> Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/qml/types')
-rw-r--r--src/qml/types/qqmltablemodel.cpp62
-rw-r--r--src/qml/types/qqmltablemodel_p.h8
2 files changed, 67 insertions, 3 deletions
diff --git a/src/qml/types/qqmltablemodel.cpp b/src/qml/types/qqmltablemodel.cpp
index c4bddfaa62..0b1273a54b 100644
--- a/src/qml/types/qqmltablemodel.cpp
+++ b/src/qml/types/qqmltablemodel.cpp
@@ -581,6 +581,36 @@ void QQmlTableModel::setRoleDataProvider(QJSValue roleDataProvider)
emit roleDataProviderChanged();
}
+/*!
+ \qmlmethod QModelIndex TableModel::index(int row, int column)
+
+ Returns a \l QModelIndex object referencing the given \a row and \a column,
+ which can be passed to the data() function to get the data from that cell,
+ or to setData() to edit the contents of that cell.
+
+ \code
+ import QtQml 2.14
+ import Qt.labs.qmlmodels 1.0
+
+ TableModel {
+ id: model
+ rows: [
+ [{ fruitType: "Apple" }, { fruitPrice: 1.50 }],
+ [{ fruitType: "Orange" }, { fruitPrice: 2.50 }]
+ ]
+ Component.onCompleted: {
+ for (var r = 0; r < model.rowCount; ++r) {
+ console.log("An " + model.data(model.index(r, 0)).fruitType +
+ " costs " + model.data(model.index(r, 1)).fruitPrice.toFixed(2))
+ }
+ }
+ }
+ \endcode
+
+ \sa {QModelIndex and related Classes in QML}, data()
+*/
+// Note: we don't document the parent argument, because you never need it, because
+// cells in a TableModel don't have parents. But it is there because this function is an override.
QModelIndex QQmlTableModel::index(int row, int column, const QModelIndex &parent) const
{
return row >= 0 && row < rowCount() && column >= 0 && column < columnCount() && !parent.isValid()
@@ -622,6 +652,22 @@ int QQmlTableModel::columnCount(const QModelIndex &parent) const
return mColumnCount;
}
+/*!
+ \qmlmethod variant TableModel::data(QModelIndex index, string role)
+
+ Returns the data from the table cell at the given \a index belonging to the
+ given \a role.
+
+ \sa index()
+*/
+QVariant QQmlTableModel::data(const QModelIndex &index, const QString &role) const
+{
+ const int iRole = mRoleNames.key(role.toUtf8(), -1);
+ if (iRole >= 0)
+ return data(index, iRole);
+ return QVariant();
+}
+
QVariant QQmlTableModel::data(const QModelIndex &index, int role) const
{
const int row = index.row();
@@ -662,6 +708,22 @@ QVariant QQmlTableModel::data(const QModelIndex &index, int role) const
return value;
}
+/*!
+ \qmlmethod bool TableModel::setData(QModelIndex index, string role, variant value)
+
+ Inserts or updates the data field named by \a role in the table cell at the
+ given \a index with \a value. Returns true if sucessful, false if not.
+
+ \sa index()
+*/
+bool QQmlTableModel::setData(const QModelIndex &index, const QString &role, const QVariant &value)
+{
+ const int iRole = mRoleNames.key(role.toUtf8(), -1);
+ if (iRole >= 0)
+ return setData(index, value, iRole);
+ return false;
+}
+
bool QQmlTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
const int row = index.row();
diff --git a/src/qml/types/qqmltablemodel_p.h b/src/qml/types/qqmltablemodel_p.h
index 863c592678..5cb42b2cc8 100644
--- a/src/qml/types/qqmltablemodel_p.h
+++ b/src/qml/types/qqmltablemodel_p.h
@@ -85,11 +85,13 @@ public:
QJSValue roleDataProvider() const;
void setRoleDataProvider(QJSValue roleDataProvider);
- QModelIndex index(int row, int column, const QModelIndex &parent) const override;
+ Q_INVOKABLE QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
- QVariant data(const QModelIndex &index, int role) const override;
- bool setData(const QModelIndex &index, const QVariant &value, int role) override;
+ Q_INVOKABLE QVariant data(const QModelIndex &index, const QString &role) const;
+ QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
+ Q_INVOKABLE bool setData(const QModelIndex &index, const QString &role, const QVariant &value);
+ bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole) override;
QHash<int, QByteArray> roleNames() const override;
Q_SIGNALS: