Address Book Example▲

This example provides an address book that allows contacts to be grouped alphabetically into 9 groups: ABC, DEF, GHI, ... , VW, ..., XYZ. This is achieved by using multiple views on the same model, each of which is filtered using an instance of the QSortFilterProxyModel class.
Overview▲
The address book contains 5 classes: MainWindow, AddressWidget, TableModel, NewAddressTab and AddDialog. The MainWindow class uses AddressWidget as its central widget and provides File and Tools menus.

The AddressWidget class is a QTabWidget subclass that is used to manipulate the 10 tabs displayed in the example: the 9 alphabet group tabs and an instance of NewAddressTab. The NewAddressTab class is a subclass of QWidget that is only used whenever the address book is empty, prompting the user to add some contacts. AddressWidget also interacts with an instance of TableModel to add, edit and remove entries to the address book.
TableModel is a subclass of QAbstractTableModel that provides the standard model/view API to access data. It holds a list of added contacts. However, this data is not all visible in a single tab. Instead, QTableView is used to provide 9 different views of the same data, according to the alphabet groups.
QSortFilterProxyModel is the class responsible for filtering the contacts for each group of contacts. Each proxy model uses a QRegExp to filter out contacts that do not belong in the corresponding alphabetical group. The AddDialog class is used to obtain information from the user for the address book. This QDialog subclass is instantiated by NewAddressTab to add contacts, and by AddressWidget to add and edit contacts.
We begin by looking at the TableModel implementation.
TableModel Class Definition▲
The TableModel class provides standard API to access data in its list of contacts by subclassing QAbstractTableModel. The basic functions that must be implemented in order to do so are: rowCount(), columnCount(), data(), headerData(). For TableModel to be editable, it has to provide implementations insertRows(), removeRows(), setData() and flags() functions.
struct
Contact
{
QString name;
QString address;
bool
operator
==
(const
Contact &
amp;other) const
{
return
name ==
other.name &
amp;&
amp; address ==
other.address;
}
}
;
inline
QDataStream &
amp;operator
&
lt;&
lt;(QDataStream &
amp;stream, const
Contact &
amp;contact)
{
return
stream &
lt;&
lt; contact.name &
lt;&
lt; contact.address;
}
inline
QDataStream &
amp;operator
&
gt;&
gt;(QDataStream &
amp;stream, Contact &
amp;contact)
{
return
stream &
gt;&
gt; contact.name &
gt;&
gt; contact.address;
}
class
TableModel : public
QAbstractTableModel
{
Q_OBJECT
public
:
TableModel(QObject *
parent =
nullptr
);
TableModel(const
QVector&
lt;Contact&
gt; &
amp;contacts, QObject *
parent =
nullptr
);
int
rowCount(const
QModelIndex &
amp;parent) const
override
;
int
columnCount(const
QModelIndex &
amp;parent) const
override
;
QVariant data(const
QModelIndex &
amp;index, int
role) const
override
;
QVariant headerData(int
section, Qt::
Orientation orientation, int
role) const
override
;
Qt::
ItemFlags flags(const
QModelIndex &
amp;index) const
override
;
bool
setData(const
QModelIndex &
amp;index, const
QVariant &
amp;value, int
role =
Qt::
EditRole) override
;
bool
insertRows(int
position, int
rows, const
QModelIndex &
amp;index =
QModelIndex()) override
;
bool
removeRows(int
position, int
rows, const
QModelIndex &
amp;index =
QModelIndex()) override
;
const
QVector&
lt;Contact&
gt; &
amp;getContacts() const
;
private
:
QVector&
lt;Contact&
gt; contacts;
}
;
Two constructors are used, a default constructor which uses TableModel's own QVector<Contact> and one that takes QVector<Contact> as an argument, for convenience.
TableModel Class Implementation▲
We implement the two constructors as defined in the header file. The second constructor initializes the list of contacts in the model, with the parameter value.
TableModel::
TableModel(QObject *
parent)
:
QAbstractTableModel(parent)
{
}
TableModel::
TableModel(const
QVector&
lt;Contact&
gt; &
amp;contacts, QObject *
parent)
:
QAbstractTableModel(parent),
contacts(contacts)
{
}
The rowCount() and columnCount() functions return the dimensions of the model. Whereas, rowCount()'s value will vary depending on the number of contacts added to the address book, columnCount()'s value is always 2 because we only need space for the Name and Address columns.
int
TableModel::
rowCount(const
QModelIndex &
amp;parent) const
{
return
parent.isValid() ? 0
: contacts.size();
}
int
TableModel::
columnCount(const
QModelIndex &
amp;parent) const
{
return
parent.isValid() ? 0
: 2
;
}
The data() function returns either a Name or Address, based on the contents of the model index supplied. The row number stored in the model index is used to reference an item in the list of contacts. Selection is handled by the QItemSelectionModel, which will be explained with AddressWidget.
QVariant TableModel::
data(const
QModelIndex &
amp;index, int
role) const
{
if
(!
index.isValid())
return
QVariant();
if
(index.row() &
gt;=
contacts.size() ||
index.row() &
lt; 0
)
return
QVariant();
if
(role ==
Qt::
DisplayRole) {
const
auto
&
amp;contact =
contacts.at(index.row());
switch
(index.column()) {
case
0
:
return
contact.name;
case
1
:
return
contact.address;
default
:
break
;
}
}
return
QVariant();
}