Resetting a Qabstractlistmodel

Asked

Viewed 67 times

1

Would you like to know how to reset or clean data from a Qabstractlistmodel? I am using Qt version 5.6.

The problem I’m 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 it, only some data may not be entered again, so I would need to perform data cleanup. But when I use what I’ve built it works for the first time it cleans the data and reinserts normally. The 2nd he cleans tbm but when I close the program he error of SIGSEGV and the error in the atomic_base. h

But what I’ve come to realize is that when I comment on this excerpt this->Foos->clear();, in the way clear class Foomodel, no longer gives 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;
}
  • If you prefer, paste the code over it, mark it with the mouse and press the control+k that the system already formats the block. Or, if you prefer, has the button { } in the edit bar that does the same.

  • It was beautiful now. I’m deleting my comments from here to not leave mess in your question. Qq doubt leave a comment that you always have someone to help. And there’s also the chat: http://chat.stackexchange.com/rooms/11910/stackoverflow

  • it does not appear this option in code insight and also ta error while compiling, I already looked in several forums and they give me this method, but I could not find.

  • See if you are applying on the right object. The reset is on the same model. http://doc.qt.io/qt-4.8/qabstractitemmodel.html#reset (I quickly got the documentation from 4.8, but I don’t think this changed in 5)

  • i took a look at Qabstractitemmodel and only show me the following methods http://prntscr.com/bh50en and Qabstractlistmodel http://prntscr.com/bh50od

  • In Qt 5, this method was discontinued http://doc.qt.io/qt-5/qabstractitemmodel-obsoletehtml

  • Yeah, it’s really changed. Just to make the question more complete, it would be nice to add in your text a description of the error you are making, or an explanation of what happens to your current code. Maybe it’ll help someone find the problem.

Show 2 more comments

1 answer

1


Well I was able 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();
}

Full 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.