2013-06-06 2 views
4

QStandardItemModel 안에 QTableView을 사용하고 있습니다. 여기에 두 개의 버튼이 있습니다 & QTableView 내 메인 윈도우 안에 있습니다. 행은 모델 내에서 달라집니다. 두 개의 버튼을 사용하여 행을 추가/삭제할 수 있습니다 (테스트 케이스). 모델에 행을 추가QStandardItemModel - 행 삭제

ADD button를 들어, 슬롯을하고있다 : -

void MainWindow::on_pushButton_clicked() 
{ 
    model->insertRow(model->rowCount()); 
} 

하지만이 Delete button를 들어, 모델에서 슬롯을 행을 삭제하고 때 내 프로그램이 충돌한다 : -

void MainWindow::on_pushButton_2_clicked() 
{ 
    QModelIndexList indexes = ui->tableView->selectionModel()->selection().indexes(); 
    QModelIndex index = indexes.at(0); 
    model->removeRows(index.row(),1); 

} 

삭제 작업을하려면 코드에서 변경해야 할 것을 제안하십시오.

편집 :

----

가 작동을 얻었다. 당신이 선택하지 않고 행을 삭제하려고 -

QModelIndex currentIndex = ui->tableView->selectionModel()->currentIndex(); 
model->removeRow(currentIndex.row()); 
+1

'indexes.size()'는 무엇을 보여줍니까? – Amartel

+0

@Amartel 응용 프로그램이 깨지고 있습니다. – Katoch

+0

작동 방법을 제안 하시겠습니까? – Katoch

답변

2

나의 제안이다. 이것을 시도하십시오 :

void MainWindow::on_pushButton_2_clicked() 
{ 
    QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows(); 
    while (!indexes.isEmpty()) 
    { 
     model->removeRows(indexes.last().row(), 1); 
     indexes.removeLast(); 
    } 
}