工作中没有小事:点石成金,滴水成河,只有认真对待自己所做的一切事情,才能克服万难,取得成功。
转载:https://blog.csdn.net/ljqiankun/article/details/50970423
在项目中使用QT的QItemSelectionModel获取QModelIndexList时程序崩溃。
使用场景:
QItemSelectionModel *selections =pTreeView->selectionModel(); QModelIndexList selectedindexes = selections->selectedRows();
这样调用后就会报错。
解决方法:找到selectedRows的源码,自己实现当前选中项
QItemSelectionModel *selections = pTreeView->selectionModel(); QModelIndexList selectedindexes; //the QSet contains pairs of parent modelIndex //and row number QSet<QPair<QModelIndex, int>> rowsSeen; const QItemSelection ranges = selections->selection(); for (int i = 0; i < ranges.count(); ++i) { const QItemSelectionRange& range = ranges.at(i); QModelIndex parent = range.parent(); for (int row = 0; i < range.top(); row <= range.bottom(); row++) { QPair<QModelIndex, int> rowDef = qMakePair(parent, row); if (!rowsSeen.contains(rowDef)) { rowsSeen << rowDef; if (selections->isRowSelected(row, parent)) { selectedindexes.append(pModel->index(row, 0, parent)); } } } }