From caffa1c18a90f387dae43ca221dac1c9eb5a41d8 Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Thu, 15 Jan 2026 14:04:37 +0100 Subject: [PATCH] Added a ToStringRole to summarize the model items (for tool tip and QR code). --- model/metadata.h | 10 +++++++++- model/modelitem.cpp | 14 ++++++++++++++ model/modelitem.h | 2 +- model/tablemodel.cpp | 2 ++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/model/metadata.h b/model/metadata.h index 97f7d4d..b0bb4b8 100644 --- a/model/metadata.h +++ b/model/metadata.h @@ -7,7 +7,15 @@ #include /// model data -enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole }; +enum UserRoles { + NameRole = Qt::UserRole + 1, + DescriptionRole, + InfoRole, + AmountRole, + FactorRole, + /// helper roles + ToStringRole +}; static UserRoles DEFAULT_ROLE = NameRole; static QList USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, AmountRole, FactorRole}; diff --git a/model/modelitem.cpp b/model/modelitem.cpp index 32bc401..a096705 100644 --- a/model/modelitem.cpp +++ b/model/modelitem.cpp @@ -44,6 +44,20 @@ bool ModelItem::setItemData(const QMap& changedValues) { return valueChanged; } +QString ModelItem::toString() const { + QString result; + + QListIterator i(USER_FACING_ROLES); + while (i.hasNext()) { + const UserRoles role = i.next(); + const QString roleName = ROLE_NAMES.value(role); + const QVariant value = data(role); + // result.append(value.toString()); + result.append(QString("%1: %2\n").arg(roleName, data(role).toString())); + } + return result; +} + QJsonObject ModelItem::toJsonObject() const { QJsonObject itemObject; // TODO add UUID and dates (entry, modification, end) diff --git a/model/modelitem.h b/model/modelitem.h index 9078c33..5d2e956 100644 --- a/model/modelitem.h +++ b/model/modelitem.h @@ -14,7 +14,7 @@ class ModelItem { // TODO change return value to list of changed roles bool setItemData(const QMap& changedValues); - // QString toString() const; + QString toString() const; QJsonObject toJsonObject() const; private: diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index 4895e9a..8519e52 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -76,6 +76,8 @@ QVariant TableModel::data(const QModelIndex& index, int role) const { case AmountRole: case FactorRole: return m_items.at(row)->data(role); + case ToStringRole: + return m_items.at(row)->toString(); } return QVariant();