2012-10-19 97 views
3

我試圖刪除QTableView中的所有選定的指標,刪除QStandardItemModel的多個索引的正確方法是什麼?

現在我用:

foreach (const QModelIndex & idx, model->selectionModel()->selectedIndexes()) 
{ 
    model->removeRow (idx.row()); // Obviously bug 
} 

還有,一旦你刪除行一個明顯的問題,該行ID無效,W

由於沒有直接使用索引的函數(或者索引是否像迭代器那樣在數據改變時會失效),我不知道該怎麼做。

回答

7

有保持索引有效狀態的QPersistanceModelIndex類。我試過了,它似乎在工作。

QList<QPersistentModelIndex> indexes; 

foreach (const QModelIndex &i, ui->tableView->selectionModel()->selectedIndexes()) 
    indexes << i; 

foreach (const QPersistentModelIndex &i, indexes) 
    ui->tableView->model()->removeRow(i.row()); 

我希望這會有所幫助。

+0

如果行號不連續會怎麼樣? – daisy

+0

@ warl0ck最初我還沒有理解你的問題。我很抱歉。現在我更新了答案,請檢查它。 – fasked

相關問題