工作中沒有小事:點石成金,滴水成河,只有認真對待自己所做的一切事情,才能克服萬難,取得成功。
轉載: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)); } } } }