Ошибка: allocated an object of abstract class type 'StringListModel'
Я видел, что экземпляр абстрактного класса создавать нельзя. Но я делаю всё по официальной документации: https://doc.qt.io/qt-6/model-view-programming.html#using-models-and-views
Объясните мне пожалуйста почему появляется ошибка. И нужно ли мне переопределить все виртуальные функции, присутствующие внутри StringListModel? Если да, то поясните почему.
Не судите строго, только начал разбираться в программировании модели/представления.
main.cpp файл
#include "mainwindow.h"
#include "stringlistmodel.h"
#include <QApplication>
#include <QtWidgets>
#include <QAbstractListModel>
#include <QModelIndex>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStringList numbers;
numbers << "One" << "Two" << "Three" << "Four" << "Five";
QAbstractItemModel *model = new StringListModel(numbers);
QListView *view = new QListView;
view->setModel(model);
view->show();
QTableView *tableView = new QTableView;
tableView->setModel(model);
tableView->show();
tableView->setSelectionModel(view->selectionModel());
return a.exec();
}
stringlistmodel.h
#ifndef STRINGLISTMODEL_H
#define STRINGLISTMODEL_H
#include <QAbstractListModel>
class StringListModel : public QAbstractItemModel
{
Q_OBJECT
public:
StringListModel(const QStringList &strings, QObject *parent = 0)
: QAbstractItemModel(parent), stringList(strings) {}
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value,int role = Qt::EditRole);
bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());
private:
QStringList stringList;
};
#endif // STRINGLISTMODEL_H