QML中ListView-GridView等对象的数据模型更新详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Shado_walker/article/details/64482334
(1)具体的行更新
QModelIndex modelIndex = this->index(indexItem, 0, QModelIndex());
emit this->dataChanged(modelIndex, modelIndex);
其中indexItem为要更新的行索引。
同理,具体列更新如下:
QModelIndex modelIndex = this->index(0, indexItem, QModelIndex());
emit this->dataChanged(modelIndex, modelIndex);
其中indexItem为要更新的列索引。


(2)插入时触发行更新
beginInsertRows(QModelIndex(), pos, pos);
m_datas.insert(pos, itemInfo);
endInsertRows();
其中pos为要插入的位置,即在ListView中的第几项。itemInfo为要插入的内容。
同理,插入时触发列更新如下:
void beginInsertColumns(const QModelIndex &parent, int first, int last);
void endInsertColumns();


(4)整体更新
void beginResetModel();
void endResetModel();


(5)移除行后的更新
void beginRemoveRows(const QModelIndex &parent, int first, int last);
void endRemoveRows();
同理,移除列后的更新如下:
void beginRemoveColumns(const QModelIndex &parent, int first, int last);
void endRemoveColumns();


(6)移动行后的更新
bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow);
void endMoveRows();
同理,移动列后的更新如下:
bool beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationColumn);

void endMoveColumns();


通过以上的几种更新方式,可以在数据模型变化后,实时调用更新函数将改变的数据展示到界面上去。这里需要注意的是,如果不是所有数据都变化了,则可以具体到只更新有改变的项,例如某行改变了,则只需要调用行改变的函数:
QModelIndex modelIndex = this->index(indexItem, 0, QModelIndex());
emit this->dataChanged(modelIndex, modelIndex);
仅仅将第indexItem项进行更新,这样做就不会引起整个数据的刷新,从而增加用户的体验。同理,其他的更新只需调用相应的更新函数即可。


猜你喜欢

转载自blog.csdn.net/Shado_walker/article/details/64482334
今日推荐