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 --- .../widgets/itemviews/addressbook/addresswidget.cpp | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'examples/widgets/itemviews/addressbook/addresswidget.cpp') 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] -- cgit v1.2.3