c++ - removeRows() and QPersistentModelIndex -
i have implemented own qabstractlistmodel
based on std::vector
. want display contents of model in qgraphicsscene
. have implemented own qgraphicsitem
stores qpersistentmodelindex
pointer data.
i have implemented removerows
method follows:
bool vectormodel::removerows(int row, int count, const qmodelindex& parent) { if (row + count < _vector.size()) { beginremoverows(qmodelindex(), row, row + count); _vector.erase(_vector.begin() + row, _vector.begin() + row + count); endremoverows(); return true; } return false; }
now since erase elements, index of following elements change. because of qpersistentmodelindex
needs adjusted.
i have found changepersistentindex()
method in qabstractitemmodel
, know can persistent indices persistentindexlist()
. don't know how adjust indices accordingly using method. how can done?
will changing these indices enough prevent invalid index
errors?
update
i have changed removerows()
enhancements of @sebastian lange, still not working expected , receive invalid index
errors:
bool labelmodel::removerows(int row, int count, const qmodelindex& parent) { q_unused(parent) if (row + count < _vector.size()) { beginremoverows(qmodelindex(), row, row + count); _vector.erase(_vector.begin() + row, _vector.begin() + row + count); endremoverows(); auto pil = persistentindexlist(); for(int = 0; < pil.size(); ++i) { if (i >= row + count) { changepersistentindex(pil[i], pil[i-count]); } } return true; } return false; }
the emitted errors (when removing 7th element):
qabstractitemmodel::endremoverows: invalid index ( 7 , 1 ) in model qabstractlistmodel(0x101559320) qabstractitemmodel::endremoverows: invalid index ( 8 , 1 ) in model qabstractlistmodel(0x101559320) qabstractitemmodel::endremoverows: invalid index ( 9 , 1 ) in model qabstractlistmodel(0x101559320) qabstractitemmodel::endremoverows: invalid index ( 10 , 1 ) in model qabstractlistmodel(0x101559320) qabstractitemmodel::endremoverows: invalid index ( 6 , 1 ) in model qabstractlistmodel(0x101559320)
well, not need fiddle changepersistentindex, calling beginremoverows , endremoverows automatically update persistent indexes existing on model. invalid qpersistentmodelindex should have after removing rows index on rows have been deleted
Comments
Post a Comment