diff options
Diffstat (limited to 'examples/widgets/itemviews/addressbook/tablemodel.cpp')
-rw-r--r-- | examples/widgets/itemviews/addressbook/tablemodel.cpp | 37 |
1 files changed, 17 insertions, 20 deletions
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<QPair<QString, QString> > pairs, QObject *parent) +TableModel::TableModel(QList<Contact> contacts, QObject *parent) : QAbstractTableModel(parent) + , contacts(contacts) { - listOfPairs = pairs; } //! [0] @@ -67,7 +67,7 @@ TableModel::TableModel(QList<QPair<QString, QString> > 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<QString, QString> 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<QString, QString> 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<QString, QString> 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<QString, QString> > TableModel::getList() +QList<Contact> TableModel::getContacts() const { - return listOfPairs; + return contacts; } //! [8] |