aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJędrzej Nowacki <jedrzej.nowacki@digia.com>2013-11-26 09:42:18 +0000
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-11-26 15:24:00 +0100
commit92756dc07a9cf3b77d68905ad692000552e41aa4 (patch)
tree258555a2b5e3bd110ea33d18b8dcd95298ec1852
parent1ef53b20c9829f1405b3674f19e44b52f0c1730a (diff)
Update TODO example
The example gathered a bit of dust. Especially in terms of role names handling Task-number: QTBUG-35024 Change-Id: Ie2309c654fc8f2cc5d14705ab03616af713f02f6 Reviewed-by: Frederik Gladhorn <frederik.gladhorn@digia.com>
-rw-r--r--examples/widgets/todos-cpp/doc/src/todos-cpp.qdoc21
-rw-r--r--examples/widgets/todos-cpp/todosmodel.cpp19
-rw-r--r--examples/widgets/todos-cpp/todosmodel.h2
-rw-r--r--src/enginio_client/enginiomodelbase_p.h9
4 files changed, 15 insertions, 36 deletions
diff --git a/examples/widgets/todos-cpp/doc/src/todos-cpp.qdoc b/examples/widgets/todos-cpp/doc/src/todos-cpp.qdoc
index 449ceda..7d0b689 100644
--- a/examples/widgets/todos-cpp/doc/src/todos-cpp.qdoc
+++ b/examples/widgets/todos-cpp/doc/src/todos-cpp.qdoc
@@ -53,17 +53,23 @@
and the \c completed information we need to define these two roles.
\snippet todos-cpp/todosmodel.h definition
- The newly created model is empty and define basic roles. Most roles are created dynamically,
- based on the json data-structure. They have no predefined value in the \l Qt::ItemDataRole enum.
+ By default views (for example \l QListView) use the \l{Qt::ItemDataRole} role to display or edit the content.
+ The newly created \l{EnginioModelCpp}{EnginioModel} is empty and defines basic roles. Most roles are created
+ dynamically, based on the json data-structure. They have no predefined value in the \l Qt::ItemDataRole enum.
\l{EnginioModelCpp}{EnginioModel} automatically populates itself as soon as the
\l{EnginioModel::query}{query} and \l{EnginioModel::enginio}{enginio} properties have been set.
When the data is downloaded, the model resets itself, and sets up the internal data cache and roles names.
\l{EnginioModelCpp}{EnginioModel} guesses the role names based on heuristics and it may be wrong if not all
objects received from the backend have exactly the same structure, for example a property can be missing
- in certain objects. To protect against such cases we overload \l{EnginioModel::roleNames}{roleNames()}
+ in certain objects. To protect against such cases we overload \l{EnginioModel::roleNames}{roleNames()}.
+ Overriding \l{EnginioModel::roleNames}{roleNames()} can also be used to match default Qt roles to the named
+ ones.
\snippet todos-cpp/todosmodel.cpp roleNames
+ In this example we map the \l Qt::DisplayRole and \l Qt::EditRole to the "title" property in the JSON.
+ This way the right string is shown by default and editing works as expected.
+
Remember to always call the base class implementation to avoid situations in which the internal cache is not in sync.
By default \l {EnginioModelCpp}{EnginioModel} operates on \l{QJsonValue}, and that is
@@ -87,15 +93,6 @@
\snippet todos-cpp/mainwindow.cpp assignModel
- At this point we are supposed to have a working read only View / Model setup. To be able
- to modify the model, we also need to reimplement the EnginioModel::setData function.
- By default \l QListView uses \l{Qt::ItemDataRole} when a user decides to edit
- a data content. We need to map that role to our "title" role, and call
- \l {EnginioModel::setData}{EnginioModel::setData()}
- to update the data, like in the following code snippet:
-
- \snippet todos-cpp/todosmodel.cpp setData
-
To make the application fully functional, a way to add and remove "To Dos" is needed.
To do so, we need to connect correct buttons to slots for adding a new item:
diff --git a/examples/widgets/todos-cpp/todosmodel.cpp b/examples/widgets/todos-cpp/todosmodel.cpp
index 196eb59..f44c1e5 100644
--- a/examples/widgets/todos-cpp/todosmodel.cpp
+++ b/examples/widgets/todos-cpp/todosmodel.cpp
@@ -47,11 +47,9 @@
#include <Enginio/enginioreply.h>
-//![resetRoles]
TodosModel::TodosModel(QObject *parent)
: EnginioModel(parent)
{}
-//![resetRoles]
QVariant TodosModel::headerData(int section, Qt::Orientation orientation, int role) const
{
@@ -63,9 +61,6 @@ QVariant TodosModel::headerData(int section, Qt::Orientation orientation, int ro
//![data]
QVariant TodosModel::data(const QModelIndex &index, int role) const
{
- if (role == Qt::DisplayRole)
- return EnginioModel::data(index, TitleRole).value<QJsonValue>().toString();
-
if (role == Qt::FontRole) {
bool completed = EnginioModel::data(index, CompletedRole).value<QJsonValue>().toBool();
QFont font;
@@ -82,27 +77,17 @@ QVariant TodosModel::data(const QModelIndex &index, int role) const
if (role == CompletedRole)
return EnginioModel::data(index, CompletedRole).value<QJsonValue>().toBool();
- if (role == TitleRole)
- return EnginioModel::data(index, TitleRole).value<QJsonValue>().toString();
-
// fallback to base class
return EnginioModel::data(index, role);
}
//![data]
-//![setData]
-bool TodosModel::setData(const QModelIndex &index, const QVariant &value, int role)
-{
- if (role == Qt::EditRole || role == TitleRole)
- return EnginioModel::setData(index, value, role);
-
- return false;
-}
-//![setData]
//![roleNames]
QHash<int, QByteArray> TodosModel::roleNames() const
{
QHash<int, QByteArray> roles = EnginioModel::roleNames();
roles.insert(TitleRole, "title");
+ roles.insert(Qt::DisplayRole, "title");
+ roles.insert(Qt::EditRole, "title");
roles.insert(CompletedRole, "completed");
return roles;
}
diff --git a/examples/widgets/todos-cpp/todosmodel.h b/examples/widgets/todos-cpp/todosmodel.h
index 0403817..c839753 100644
--- a/examples/widgets/todos-cpp/todosmodel.h
+++ b/examples/widgets/todos-cpp/todosmodel.h
@@ -58,8 +58,6 @@ public:
explicit TodosModel(QObject *parent = 0);
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
- virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE;
-
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE;
virtual QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;
diff --git a/src/enginio_client/enginiomodelbase_p.h b/src/enginio_client/enginiomodelbase_p.h
index b2e104d..e9eae8a 100644
--- a/src/enginio_client/enginiomodelbase_p.h
+++ b/src/enginio_client/enginiomodelbase_p.h
@@ -775,7 +775,7 @@ public:
EnginioReplyBase *setData(const int row, const QVariant &value, int role)
{
- if (role > Enginio::SyncedRole) {
+ if (role != Enginio::InvalidRole) {
QJsonObject oldObject = _data.at(row).toObject();
QString id = oldObject[EnginioString::id].toString();
if (id.isEmpty())
@@ -816,8 +816,8 @@ public:
EnginioReplyBase *setDataNow(const int row, const QVariant &value, int role, const QJsonObject &oldObject, const QString &id)
{
Q_ASSERT(!id.isEmpty());
- Q_ASSERT(role > Enginio::SyncedRole);
const QString roleName(_roles.value(role));
+ Q_ASSERT(!roleName.isEmpty());
QJsonObject deltaObject;
QJsonObject newObject = oldObject;
deltaObject[roleName] = newObject[roleName] = QJsonValue::fromVariant(value);
@@ -860,14 +860,13 @@ public:
return _attachedData.isSynced(row);
}
- if (role == Qt::DisplayRole)
- return _data.at(row);
-
const QJsonObject object = _data.at(row).toObject();
if (!object.isEmpty()) {
const QString roleName = _roles.value(role);
if (!roleName.isEmpty())
return object[roleName];
+ else if (role == Qt::DisplayRole)
+ return _data.at(row);
}
return QVariant();