Adding a character to the characters model

Now that we have made our Characters entity model/view aware, we will still need a utility method to add a new character to it. This method will be called add and will take the character's name as an argument.

We will add its declaration to characters.h:

// characters.h#ifndef CHARACTERS_H...class Characters : public QAbstractListModel{...public:   ...    void add(const QString& name);...}...

Then, we will provide its implementation in characters.cpp:

// characters.cpp...void Characters::add(const QString &name){    if (! m_list.contains(name)) {        beginInsertRows(QModelIndex(), m_list.count(), m_list.count());        m_list.append(name);        endInsertRows();    }}...

Besides the push_back method of QList (which is equivalent ...

Get End to End GUI Development with Qt5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.