私はこの問題の解決策を探していましたが、これは Qt 関数自体のバグである可能性があると信じ始めています。問題は、QItemSelectionModel::selectedIndexes() を呼び出した後、プログラムがこの関数が返す QModelIndexList を破棄しようとすると、プログラムがクラッシュすることです。クラッシュの前に、次のデバッグ メッセージが表示されます。
デバッグ アサーションに失敗しました!
(…)
ファイル: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
行:1419
式: _pFirstBlock == pHead
(…)
問題を引き起こす最も単純なコードは次のとおりです。自分でテストできます。
#include <QApplication>
#include <QStringList>
#include <QStringListModel>
#include <QItemSelectionModel>
#include <QModelIndex>
#include <QModelIndexList>
#include <QListView>
#include <QItemSelectionModel>
void doSomethingWithSelection(QItemSelectionModel* selectionmodel);
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStringList list;
list.push_back("1");
list.push_back("2");
list.push_back("3");
list.push_back("4");
QStringListModel model(list);
QListView view;
view.setModel(&model);
view.setSelectionMode(QAbstractItemView::ExtendedSelection);
QItemSelectionModel *selectionmodel = view.selectionModel();
QModelIndex first = model.index(0);
QModelIndex last = model.index(2);
QItemSelection selection(first, last);
selectionmodel->select(selection,QItemSelectionModel::Select);
doSomethingWithSelection(selectionmodel);
view.show();
return a.exec();
}
void doSomethingWithSelection(QItemSelectionModel* selectionmodel)
{
QModelIndexList indexlist = selectionmodel->selectedIndexes();
// this is what causes the error. I put this inside a function
// so the error will happen when exiting the function,
// when the program try to destroy the list nodes.
}