在我关于Qt的一个项目中,我要求从视图(树视图)等文件夹中选择多个项目,并在另一个窗口小部件中填充所选项目 . 要显示多选的可用项目,我正在使用QTreeView,并填充元素层次结构,如下所示

m_StandardModel = new QStandardItemModel ;
QStandardItem *rootNode = m_StandardModel->invisibleRootItem();

//defining a couple of items
QStandardItem *item1 =  new QStandardItem(tr("ITEM1"));
QStandardItem *item2 =  new QStandardItem(tr("ITEM2"));
QStandardItem *item3 =  new QStandardItem(tr("ITEM3"));
QStandardItem *item4 =  new QStandardItem(tr("ITEM4"));

rootNode->appendRow(item1 );
rootNode->appendRow(item2 );
rootNode->appendRow(item3 );
rootNode->appendRow(item4 );

 //register the model
ui->treeView->setModel(m_StandardModel);
ui->treeView->expandAll();

//enabling multiselection behaviour
QItemSelectionModel *selectionModel= ui->treeView->selectionModel();
ui->treeView->setSelectionMode(QAbstractItemView::MultiSelection);

一切都很好,直到这里 . 我能够在树视图中显示我的项目,并且还能够多选项目 . 当我尝试从树视图中使用这些多个选定项时,会发生此问题 .

在我的UI中,我将一个按钮的 clicked() 信号连接到我的插槽,该插槽处理所选项目的迭代和操作 . 在这里_2817313被称为:

//User selects a number of features listed on the left pane and clicks this button to  disable them
void MainWindow::on_pushButton_2_clicked()
{        
    QModelIndexList selectedItems =  ui->treeView->selectionModel()->selectedIndexes();

    QStringList items;

    foreach(QModelIndex index, selectedItems)
    {
        QStandardItemModel* itemModel = dynamic_cast<QStandardItemModel*>(ui->treeView->model());
        if(itemModel)
        {
            QStandardItem* item = itemModel->itemFromIndex(index);
            items<< item->data().toString();
        }
    }
}

调试直到功能结束是完美的 . 但是一旦我退出这个功能(如上所示),我就得到一个 DEBUG ASSERTION !!如下

HEAP[myprog.exe]: Invalid address specified to RtlValidateHeap( 00390000, 01946798 )

调用堆栈显示由于我在函数中创建的本地QModelIndexList的破坏而到达此断言 .

以下是调试断言时的调用堆栈:
enter image description here

任何想法,我可能会缺少什么?我已经尝试了很多次,但却无法弄清楚真正的问题 . 有没有更好的方法来做这里正在做的事情?我正在使用QT 4.8.4,并且正在Windows 7上的 DEBUG 配置中构建/调试我的应用程序 .