qml listview with c++ nest listmodel crash when pop from stackview
-
i have a c++ nest listmodel structure like this
class ListModel : public AbstractListModel
{
public:
.....
private:
QList<QObject*> m_List;
}class TrendListModel : public ListModel
{
public:
.....
void addTrendData(.....)
{
TrendData* trendData = new TrendData(....);QQmlEngine::setObjectOwnership(trendData, QQmlEngine::CppOwnership); beginInsertRows(QModelIndex(), rowCount(), rowCount()); m_List << trendData; endInsertRows(); } void addImage(int row, QString path) { if(row < 0 || row >= rowCount()) { qWarning() << "addImage to invalid trendData pointer!" return; } TrendData* trendData = qobject_cast<TrendData*>(m_List.at(row)); trendData->addImage(...., path); emit dataChanged(index(row), index(row)); }
}
class TrendData : public QObject
{
public:
.....
{
m_ImageListModel = new ImageListModel(this);
QQmlEngine::setObjectOwnership(m_ImageListModel, QQmlEngine::CppOwnership);
}
void addImage(..., path)
{
m_ImageListModel->addImage(..., path);
}
private:
ImageListModel* m_ImageListModel;
}class ImageListModel : public ListModel
{
void addImage(..., path)
{
Image* image = new Image(path, this);QQmlEngine::setObjectOwnership(image, QQmlEngine::CppOwnership); beginInsertRows(QModelIndex(), rowCount(), rowCount()); m_List << image; endInsertRows(); }
}
ListView{
model: trendListModel //from setContextProperty()delegate{ ....... ListView{ model: imageListModel //return from trendListModel's data function } }
}
i append the instance of TrendListModel to qml listview, preview correctly, but when i pop the qml page from stackview, it crash on ios, but works fine on android. Attact crash report as below:
-
Order code follow:
cpp:
//~ ListModel AbstractListModel { QList<QObject*> m_List; } //~ TrendData QObject{ ImageListModel* m_ImageListModel; function addImage(..., path){ m_ImageListModel->addImage(..., path); } } //~ TrendListModel ListModel { function addTrendData(){ var trendData = new TrendData(); begineInsertRows(); this->m_list << trendData; endInsertRows(); } function addImage(row, path){ if(something is not ok) return; var trendData = m_list.at(row); // here check trendData vaild. // if qobject_cast fail. //! [0] if(trendData == null) return; //! [0] trendData->addImage(..., path); emit dataChanged(index(row), index(row)); } } class Image; //~ ImageListModel ListModel { function addImage(..., path){ var image = new Image; beginInserRows(); this->m_list << image; endInsertRows(); } }
qml:
ListView { model: trendListModel // from setContextProperty() delegate: ListView { model: imageListModel //return from trendListModel's data function } }