2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#ifndef TABLEMODEL_H
|
|
|
|
#define TABLEMODEL_H
|
|
|
|
|
|
|
|
#include <QAbstractTableModel>
|
2020-06-22 08:12:38 +00:00
|
|
|
#include <QList>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
//! [0]
|
2017-09-04 15:09:52 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
class TableModel : public QAbstractTableModel
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
public:
|
2018-11-12 20:20:34 +00:00
|
|
|
TableModel(QObject *parent = nullptr);
|
2020-06-22 08:12:38 +00:00
|
|
|
TableModel(const QList<Contact> &contacts, QObject *parent = nullptr);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-06-15 08:12:35 +00:00
|
|
|
int rowCount(const QModelIndex &parent) const override;
|
|
|
|
int columnCount(const QModelIndex &parent) const override;
|
|
|
|
QVariant data(const QModelIndex &index, int role) const override;
|
|
|
|
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
|
|
|
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
|
|
|
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;
|
2020-06-22 08:12:38 +00:00
|
|
|
const QList<Contact> &getContacts() const;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
private:
|
2020-06-22 08:12:38 +00:00
|
|
|
QList<Contact> contacts;
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
//! [0]
|
|
|
|
|
2012-11-21 14:45:13 +00:00
|
|
|
#endif // TABLEMODEL_H
|