O'Reilly logo

Qt 5 Projects by Marco Piccolino

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required