From 66119a07e840daae61629762ad3763abd0c16754 Mon Sep 17 00:00:00 2001 From: Alexander Volkov Date: Mon, 4 Sep 2017 18:09:52 +0300 Subject: Address Book example: Replace QPair by struct Introduce Contact struct to store contact data and use it instead of QPair. Proper naming really clarifies the code. Task-number: QTBUG-60635 Change-Id: Ibfb421dfc854accc382212b0da46e7aafc0d528a Reviewed-by: Jesus Fernandez Reviewed-by: Friedemann Kleint --- .../itemviews/addressbook/addresswidget.cpp | 20 ++++-------- .../widgets/itemviews/addressbook/tablemodel.cpp | 37 ++++++++++------------ .../widgets/itemviews/addressbook/tablemodel.h | 29 ++++++++++++++--- 3 files changed, 49 insertions(+), 37 deletions(-) (limited to 'examples/widgets/itemviews') diff --git a/examples/widgets/itemviews/addressbook/addresswidget.cpp b/examples/widgets/itemviews/addressbook/addresswidget.cpp index cff35ad78f..9480d2ca8e 100644 --- a/examples/widgets/itemviews/addressbook/addresswidget.cpp +++ b/examples/widgets/itemviews/addressbook/addresswidget.cpp @@ -85,10 +85,7 @@ void AddressWidget::showAddEntryDialog() //! [3] void AddressWidget::addEntry(QString name, QString address) { - QList >list = table->getList(); - QPair pair(name, address); - - if (!list.contains(pair)) { + if (!table->getContacts().contains({ name, address })) { table->insertRows(0, 1, QModelIndex()); QModelIndex index = table->index(0, 0, QModelIndex()); @@ -211,18 +208,16 @@ void AddressWidget::readFromFile(const QString &fileName) return; } - QList > pairs = table->getList(); + QList contacts; QDataStream in(&file); - in >> pairs; + in >> contacts; - if (pairs.isEmpty()) { + if (contacts.isEmpty()) { QMessageBox::information(this, tr("No contacts in file"), tr("The file you are attempting to open contains no contacts.")); } else { - for (int i=0; i p = pairs.at(i); - addEntry(p.first, p.second); - } + for (const auto &contact: qAsConst(contacts)) + addEntry(contact.name, contact.address); } } //! [7] @@ -237,8 +232,7 @@ void AddressWidget::writeToFile(const QString &fileName) return; } - QList > pairs = table->getList(); QDataStream out(&file); - out << pairs; + out << table->getContacts(); } //! [6] diff --git a/examples/widgets/itemviews/addressbook/tablemodel.cpp b/examples/widgets/itemviews/addressbook/tablemodel.cpp index d701ef3223..674e312753 100644 --- a/examples/widgets/itemviews/addressbook/tablemodel.cpp +++ b/examples/widgets/itemviews/addressbook/tablemodel.cpp @@ -56,10 +56,10 @@ TableModel::TableModel(QObject *parent) { } -TableModel::TableModel(QList > pairs, QObject *parent) +TableModel::TableModel(QList contacts, QObject *parent) : QAbstractTableModel(parent) + , contacts(contacts) { - listOfPairs = pairs; } //! [0] @@ -67,7 +67,7 @@ TableModel::TableModel(QList > pairs, QObject *parent) int TableModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); - return listOfPairs.size(); + return contacts.size(); } int TableModel::columnCount(const QModelIndex &parent) const @@ -83,16 +83,16 @@ QVariant TableModel::data(const QModelIndex &index, int role) const if (!index.isValid()) return QVariant(); - if (index.row() >= listOfPairs.size() || index.row() < 0) + if (index.row() >= contacts.size() || index.row() < 0) return QVariant(); if (role == Qt::DisplayRole) { - QPair pair = listOfPairs.at(index.row()); + const auto &contact = contacts.at(index.row()); if (index.column() == 0) - return pair.first; + return contact.name; else if (index.column() == 1) - return pair.second; + return contact.address; } return QVariant(); } @@ -126,10 +126,8 @@ bool TableModel::insertRows(int position, int rows, const QModelIndex &index) Q_UNUSED(index); beginInsertRows(QModelIndex(), position, position + rows - 1); - for (int row = 0; row < rows; ++row) { - QPair pair(" ", " "); - listOfPairs.insert(position, pair); - } + for (int row = 0; row < rows; ++row) + contacts.insert(position, { QString(), QString() }); endInsertRows(); return true; @@ -142,9 +140,8 @@ bool TableModel::removeRows(int position, int rows, const QModelIndex &index) Q_UNUSED(index); beginRemoveRows(QModelIndex(), position, position + rows - 1); - for (int row = 0; row < rows; ++row) { - listOfPairs.removeAt(position); - } + for (int row = 0; row < rows; ++row) + contacts.removeAt(position); endRemoveRows(); return true; @@ -157,16 +154,16 @@ bool TableModel::setData(const QModelIndex &index, const QVariant &value, int ro if (index.isValid() && role == Qt::EditRole) { int row = index.row(); - QPair p = listOfPairs.value(row); + auto contact = contacts.value(row); if (index.column() == 0) - p.first = value.toString(); + contact.name = value.toString(); else if (index.column() == 1) - p.second = value.toString(); + contact.address = value.toString(); else return false; - listOfPairs.replace(row, p); + contacts.replace(row, contact); emit(dataChanged(index, index)); return true; @@ -187,8 +184,8 @@ Qt::ItemFlags TableModel::flags(const QModelIndex &index) const //! [7] //! [8] -QList< QPair > TableModel::getList() +QList TableModel::getContacts() const { - return listOfPairs; + return contacts; } //! [8] diff --git a/examples/widgets/itemviews/addressbook/tablemodel.h b/examples/widgets/itemviews/addressbook/tablemodel.h index 9a669c508d..1004a35d31 100644 --- a/examples/widgets/itemviews/addressbook/tablemodel.h +++ b/examples/widgets/itemviews/addressbook/tablemodel.h @@ -53,16 +53,37 @@ #include #include -#include //! [0] + +struct Contact +{ + QString name; + QString address; + + bool operator==(const Contact &other) const + { + return name == other.name && address == other.address; + } +}; + +inline QDataStream &operator<<(QDataStream &stream, const Contact &contact) +{ + return stream << contact.name << contact.address; +} + +inline QDataStream &operator>>(QDataStream &stream, Contact &contact) +{ + return stream >> contact.name >> contact.address; +} + class TableModel : public QAbstractTableModel { Q_OBJECT public: TableModel(QObject *parent = 0); - TableModel(QList > listofPairs, QObject *parent = 0); + TableModel(QList contacts, QObject *parent = 0); int rowCount(const QModelIndex &parent) const override; int columnCount(const QModelIndex &parent) const override; @@ -72,10 +93,10 @@ public: bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()) override; bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex()) override; - QList > getList(); + QList getContacts() const; private: - QList > listOfPairs; + QList contacts; }; //! [0] -- cgit v1.2.3