diff options
author | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2018-11-12 21:20:34 +0100 |
---|---|---|
committer | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2018-12-04 07:00:41 +0000 |
commit | 51f6d5d8c261fb94adc9bf5754cabf8e37236af5 (patch) | |
tree | 88a374c564ffadd6b1f90e97121ab7274484bf71 /examples/widgets/itemviews/addressbook/tablemodel.cpp | |
parent | 8c38b08343b226883ef368d31a085b0a620f7995 (diff) |
Cleanup Addressbook example
Cleanup the Addressbook example:
- use nullptr
- use for instead foreach
- don't use public members but setters/getters
- use QVector instead QList
- make user-visible translatable
Change-Id: Ie7bdad8a2799c8fa6f634659b51c3064cc8a04ce
Reviewed-by: Samuel Gaist <samuel.gaist@idiap.ch>
Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com>
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Luca Beldi <v.ronin@yahoo.it>
Diffstat (limited to 'examples/widgets/itemviews/addressbook/tablemodel.cpp')
-rw-r--r-- | examples/widgets/itemviews/addressbook/tablemodel.cpp | 52 |
1 files changed, 27 insertions, 25 deletions
diff --git a/examples/widgets/itemviews/addressbook/tablemodel.cpp b/examples/widgets/itemviews/addressbook/tablemodel.cpp index b3704f857e..ddf79a3fa3 100644 --- a/examples/widgets/itemviews/addressbook/tablemodel.cpp +++ b/examples/widgets/itemviews/addressbook/tablemodel.cpp @@ -56,9 +56,9 @@ TableModel::TableModel(QObject *parent) { } -TableModel::TableModel(QList<Contact> contacts, QObject *parent) - : QAbstractTableModel(parent) - , contacts(contacts) +TableModel::TableModel(const QVector<Contact> &contacts, QObject *parent) + : QAbstractTableModel(parent), + contacts(contacts) { } //! [0] @@ -66,14 +66,12 @@ TableModel::TableModel(QList<Contact> contacts, QObject *parent) //! [1] int TableModel::rowCount(const QModelIndex &parent) const { - Q_UNUSED(parent); - return contacts.size(); + return parent.isValid() ? 0 : contacts.size(); } int TableModel::columnCount(const QModelIndex &parent) const { - Q_UNUSED(parent); - return 2; + return parent.isValid() ? 0 : 2; } //! [1] @@ -89,10 +87,14 @@ QVariant TableModel::data(const QModelIndex &index, int role) const if (role == Qt::DisplayRole) { const auto &contact = contacts.at(index.row()); - if (index.column() == 0) - return contact.name; - else if (index.column() == 1) - return contact.address; + switch (index.column()) { + case 0: + return contact.name; + case 1: + return contact.address; + default: + break; + } } return QVariant(); } @@ -108,12 +110,10 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation, int ro switch (section) { case 0: return tr("Name"); - case 1: return tr("Address"); - default: - return QVariant(); + break; } } return QVariant(); @@ -152,19 +152,21 @@ bool TableModel::removeRows(int position, int rows, const QModelIndex &index) bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role == Qt::EditRole) { - int row = index.row(); - + const int row = index.row(); auto contact = contacts.value(row); - if (index.column() == 0) - contact.name = value.toString(); - else if (index.column() == 1) - contact.address = value.toString(); - else - return false; - + switch (index.column()) { + case 0: + contact.name = value.toString(); + break; + case 1: + contact.address = value.toString(); + break; + default: + return false; + } contacts.replace(row, contact); - emit dataChanged(index, index, {role}); + emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); return true; } @@ -184,7 +186,7 @@ Qt::ItemFlags TableModel::flags(const QModelIndex &index) const //! [7] //! [8] -QList<Contact> TableModel::getContacts() const +const QVector<Contact> &TableModel::getContacts() const { return contacts; } |