Talei
14th April 2010, 00:44
Hello,
I have problem with selection inside model/views (ListView and TableView). The problem is that I want to remove from QStandardItemModel selected rows, everything works fine, except that some selected items are not removed. I spend few days looking at the code/reading but couldn't find solution.
So I do this:
assign model and itemselectionmodel to views (for clarity sake i post only TableView code) like this:
tableView->setModel( &model ); //setup model before show()
tableView->setSelectionBehavior( QAbstractItemView::SelectRows );
smodelTable = new QItemSelectionModel( &model );
tableView->setSelectionModel( smodelTable ); //I use atm. two separate selectionmodels only to figure out the problem
then, after item are selected, I use this code to remove selected rows from model:
void MainWindow::clearSelectedItemsSlot()
{
QModelIndexList selIndeModel;
if( listView->isVisible()){
selIndeModel = smodelList->selectedIndexes(); //for listView
}else{
selIndeModel = smodelTable->selectedRows(); //for tableView
}
for( int i = 0; i < selIndeModel.size(); ++i )
{
model.removeRow( selIndeModel[i].row() );
fileNames.removeAt( selIndeModel[i].row() );
}
selIndeModel.clear(); //for test purpose
smodelList->clearSelection();
smodelTable->clearSelection();
}
Items are indeed removed, but sometimes one stays in model, sometimes one in middle, depending what was selected.
I can't think of any reason why this is happening.
Any suggestion are more then welcome.
PS> This behaviour is same for both listView and tableView, so I suspect that my remove code is wrong
I have problem with selection inside model/views (ListView and TableView). The problem is that I want to remove from QStandardItemModel selected rows, everything works fine, except that some selected items are not removed. I spend few days looking at the code/reading but couldn't find solution.
So I do this:
assign model and itemselectionmodel to views (for clarity sake i post only TableView code) like this:
tableView->setModel( &model ); //setup model before show()
tableView->setSelectionBehavior( QAbstractItemView::SelectRows );
smodelTable = new QItemSelectionModel( &model );
tableView->setSelectionModel( smodelTable ); //I use atm. two separate selectionmodels only to figure out the problem
then, after item are selected, I use this code to remove selected rows from model:
void MainWindow::clearSelectedItemsSlot()
{
QModelIndexList selIndeModel;
if( listView->isVisible()){
selIndeModel = smodelList->selectedIndexes(); //for listView
}else{
selIndeModel = smodelTable->selectedRows(); //for tableView
}
for( int i = 0; i < selIndeModel.size(); ++i )
{
model.removeRow( selIndeModel[i].row() );
fileNames.removeAt( selIndeModel[i].row() );
}
selIndeModel.clear(); //for test purpose
smodelList->clearSelection();
smodelTable->clearSelection();
}
Items are indeed removed, but sometimes one stays in model, sometimes one in middle, depending what was selected.
I can't think of any reason why this is happening.
Any suggestion are more then welcome.
PS> This behaviour is same for both listView and tableView, so I suspect that my remove code is wrong