Reset a QAbstractListModel

1

I would like to know how do I reset or clear the data of a QAbstractListModel? I'm using version 5.6 of Qt.

The problem I am having is when I try to perform an update of the information that is in the Model, where I need to clean the data and reinsert them, but some data may not be inserted again, so I would need to perform the cleanups of the data. But when I use what I put together it works for the first time it cleans up the data and reinserts it normally. The 2nd it cleans, however, when I close the program of the SIGSEGV error and the error in atomic_base.h unit

But what I noticed is that when I comment on this this-> foos-> clear (); in the clear FooModel , does not give the error but the list tbm is not reset.

class FooModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum FooRoles {
        TypeRole = Qt::UserRole + 1, NameRole, PotionRole
    };
    FooModel(QObject *parent = 0);
    ~FooModel();
    void addFoo(const Foo &value);
    int rowCount(const QModelIndex & parent = QModelIndex()) const;
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    void clear();
protected:
    QHash<int, QByteArray> roleNames() const;
private:
    QList<Foo> *foos;
};

FooModel::FooModel(QObject *parent):QAbstractListModel(parent)
{
    this->foos = new QList<Foo>();
}

FooModel::~FooModel()
{
  delete(this->foos);
}

void FooModel::addFoo(const Food &value)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    foos->append(value);
    endInsertRows();
}

int FooModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return foos->count();
}

QVariant FooModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= foos->count())
        return QVariant();

    const Foo &foo = foos->at(index.row());
    switch (role) {
    case TypeRole:
        return "None";
        break;
    case NameRole:
        return foo.getName();
        break;
    case PotionRole:
        return "None";
        break;
    default:
        break;
    }
    return QVariant();
}

void FooModel::clear()
{
    beginResetModel();
    this->foos->clear();
    this->roleNames().clear();
    endResetModel();
}

QHash<int, QByteArray> FooModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[TypeRole] = "type";
    roles[NameRole] = "name";
    roles[PotionRole] = "potion";
    return roles;
}
    
asked by anonymous 16.06.2016 / 16:00

1 answer

1

Well I managed to find the solution. The problem was that the objects were not being destroyed.

The clear method

void FooModel::clear()
{
    beginResetModel();

    this->foos->clear();

    qDeleteAll(*foos);

    endResetModel();
}

Complete Example

Header

class FooModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum FooRoles {
        TypeRole = Qt::UserRole + 1, NameRole, PotionRole
    };
    FooModel(QObject *parent = 0);
    ~FooModel();
    void addFoo(Foo *value);
    int rowCount(const QModelIndex & parent = QModelIndex()) const;
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    void clear();
protected:
    QHash<int, QByteArray> roleNames() const;
private:
    QList<Foo*> *foos;
};

Cpp

FooModel::FooModel(QObject *parent):QAbstractListModel(parent)
{
    this->foos = new QList<Foo*>();
}

FooModel::~FooModel()
{
    delete(this->foos);
}

void FooModel::addFoo(Foo *value)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    foos->append(value);
    endInsertRows();
}

int FooModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return foos->count();
}

QVariant FooModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= foos->count())
        return QVariant();

    const Foo *foo = foos->at(index.row());
    switch (role) {
    case TypeRole:
        return "None";
        break;
    case NameRole:
        return foo->getName();
        break;
    case PotionRole:
        return "None";
        break;
    default:
        break;
    }
    return QVariant();
}

void FooModel::clear()
{
    beginResetModel();

    this->foos->clear();

    qDeleteAll(*foos);

    endResetModel();
}

QHash<int, QByteArray> FooModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[TypeRole] = "type";
    roles[NameRole] = "name";
    roles[PotionRole] = "potion";
    return roles;
}
    
16.06.2016 / 19:22