Solved How to reset a custom model?
-
I have a custom model that I'm studying and I was wondering, how can I reset the model and empty the items on the model and on the table view?
#ifndef RESULTSMODEL_HPP #define RESULTSMODEL_HPP #include <QAbstractTableModel> class Result; class ResultsModel : public QAbstractTableModel { Q_OBJECT public: explicit ResultsModel(QObject *parent = nullptr); int rowCount(const QModelIndex &parent) const override; int columnCount(const QModelIndex &parent) const override; QVariant data(const QModelIndex &index, int role) const override; bool setData(const QModelIndex &index, const QVariant &value, int role) override; QVariant headerData(int section, Qt::Orientation orientation, int role) const override; Qt::ItemFlags flags(const QModelIndex &index) const override; void addResult(Result *result); private: QList<Result *> mResults; }; #endif // RESULTSMODEL_HPP
The model holds the parenting of the
Result *
but I wanted that when resetting to delete the pointers as well. -
And what prevents you from deleting the pointer by yourself?
Make sure you call the functions beginResetModel/endRestModel as described in the documentation: http://doc.qt.io/qt-5/qabstractitemmodel.html#beginResetModel -
That is the thing, I never did the
beginResetModel
and I read the docs but I couldn't find an example of the usage, where to call that and so on.I tried like this but it's crashing:
void ResultsModel::reset() { beginResetModel(); qDeleteAll(mResults); endResetModel(); }
-
@Mr-Gisa said in How to reset a custom model?:
I tried like this but it's crashing:
void ResultsModel::reset() { beginResetModel(); qDeleteAll(mResults); endResetModel(); }
After you call
qDeleteAll()
, you now have a QList full of dangling pointers.See http://doc.qt.io/qt-5/qtalgorithms.html#qDeleteAll : "Notice that qDeleteAll() doesn't remove the items from the container; it merely calls delete on them. In the example above, we call clear() on the container to remove the items."
@Mr-Gisa said in How to reset a custom model?:
how can I reset the model and empty the items on the model and on the table view?
You're almost there. You just need to remove the dangling pointers. The basic steps for all models are:
- Call
beginResetModel()
to tell the views to stop reading your model - Use your custom code to reset the internal data
- Call
endResetModel()
to tell the views that the model has been reset. The views will now re-read the model to discover the updated data.
- Call
-
Use your custom code to reset the internal data
I tried to use
mResults.clear()
it worked but now I get a lot of pointers leaking.I can't call
qDeleteAll(mResults)
cause the list is empty.beginResetModel(); mResults.clear(); endResetModel(); qDeleteAll(mResults); // won't work.
-
@Mr-Gisa said in How to reset a custom model?:
I tried to use
mResults.clear()
it worked but now I get a lot of pointers leaking.I can't call
qDeleteAll(mResults)
cause the list is empty.See the example code in the documentation. I provided the link in the previous post.
-
Oh gosh, sorry, I though that you mentioned only that part of the documentation and wasn't anything there other than that, my bad
It solved, thank you very much. -
import QtQuick 2.11
import QtQuick.Window 2.11Window {
id: window visible: true width: 300 height: 480 color: "#050122" title: qsTr("sortea numeros") Grid{ spacing: 1 rows: 3 columns: 2 anchors.fill: parent Repeater { id:modelo model: 6 delegate: Rectangle { width: window.width/6; height: window.height/6; color: "darkblue"; Text { anchors.centerIn: parent font.pixelSize: 20 text: sortea() } } } } MouseArea { id: mouseArea x: 220 y: 387 anchors.fill: parent onClicked: { modelo.model=0; modelo.model=6; } } function sortea() { var random =Math.floor(Math.random() * 6) + 1; return random; }
}