xelag
27th November 2006, 00:30
Hello,
I have a problem with a Model-View-Controler.
I can't get the index of the selected items in my view. It seems that there are no connection between my model and my selection model
Here is my MVC,
model : QAbstractTableModel
view : QTableView Proxy
proxy : QSortFilterProxyModel
delegate : QAbstractItemDelegate
selection model : QItemSelectionModel
My view selection setting is set as :
setSelectionMode (QAbstractItemView::ExtendedSelection);
setSelectionBehavior (QAbstractItemView::SelectRows);
In my model, each data gets a Qt::ItemFlags as Qt::ItemIsSelectable :
Qt::ItemFlags DSTableModel::flags(const QModelIndex &_index) const
{
if (!_index.isValid())
{
return QAbstractItemModel::flags(_index) | Qt::ItemIsEnabled;
}
return QAbstractItemModel::flags(_index) | Qt::ItemIsEditable | Qt::ItemIsSelectable;
}
In my main program, i use this slot function to delete the selected rows in the view:
void DSMainWindow::deleteDataSlot()
{
//Get the QModelIndex list of items selected in the view
QModelIndexList tmpModelIndexList = selectionModel->selectedIndexes();
if(tmpModelIndexList.count()==0)
{
//---> Always comes here because no items have been detected selected, despite
//the fact that the user has selected rows for real in the view !!!!!!!!!!!!!!!!!!
QMessageBox::information(this, tr("DataSearch delete data"),
tr("No data is selected"),
QMessageBox::Ok,0,0);
return;
}
else{}
int tmpMsgRet = QMessageBox::question(this, tr("DataSearch delete data"),
tr("Are you sure you want to delete the selected data?") +
tr("\nThe data deleted will be lost."),
QMessageBox::Yes,QMessageBox::No | QMessageBox::Default,0);
if (tmpMsgRet == QMessageBox::Yes)
{
//list used to store the row already deleted (multiple QModelIndex for a row
QList<int> tmpRowRemoved;
int currentRow;
for(int i=0; i<tmpModelIndexList.count();i++)
{
currentRow = tmpModelIndexList.at(i).row();
//test if the row has not been deleted yet
if(tmpRowRemoved.indexOf(currentRow) > 0)
{
//delete the row in the model
tableModel->removeRows(currentRow,1,tableView->currentIndex());
tmpRowRemoved.append(currentRow);
}
}
}
else{}
}
Can the delegate or the proxy cause a problem? Is there something to add in the model?
Please help. I'm searching for a long time where it bugs... But those MVCs are quite tough to debug...
Thx,
Xelag
I have a problem with a Model-View-Controler.
I can't get the index of the selected items in my view. It seems that there are no connection between my model and my selection model
Here is my MVC,
model : QAbstractTableModel
view : QTableView Proxy
proxy : QSortFilterProxyModel
delegate : QAbstractItemDelegate
selection model : QItemSelectionModel
My view selection setting is set as :
setSelectionMode (QAbstractItemView::ExtendedSelection);
setSelectionBehavior (QAbstractItemView::SelectRows);
In my model, each data gets a Qt::ItemFlags as Qt::ItemIsSelectable :
Qt::ItemFlags DSTableModel::flags(const QModelIndex &_index) const
{
if (!_index.isValid())
{
return QAbstractItemModel::flags(_index) | Qt::ItemIsEnabled;
}
return QAbstractItemModel::flags(_index) | Qt::ItemIsEditable | Qt::ItemIsSelectable;
}
In my main program, i use this slot function to delete the selected rows in the view:
void DSMainWindow::deleteDataSlot()
{
//Get the QModelIndex list of items selected in the view
QModelIndexList tmpModelIndexList = selectionModel->selectedIndexes();
if(tmpModelIndexList.count()==0)
{
//---> Always comes here because no items have been detected selected, despite
//the fact that the user has selected rows for real in the view !!!!!!!!!!!!!!!!!!
QMessageBox::information(this, tr("DataSearch delete data"),
tr("No data is selected"),
QMessageBox::Ok,0,0);
return;
}
else{}
int tmpMsgRet = QMessageBox::question(this, tr("DataSearch delete data"),
tr("Are you sure you want to delete the selected data?") +
tr("\nThe data deleted will be lost."),
QMessageBox::Yes,QMessageBox::No | QMessageBox::Default,0);
if (tmpMsgRet == QMessageBox::Yes)
{
//list used to store the row already deleted (multiple QModelIndex for a row
QList<int> tmpRowRemoved;
int currentRow;
for(int i=0; i<tmpModelIndexList.count();i++)
{
currentRow = tmpModelIndexList.at(i).row();
//test if the row has not been deleted yet
if(tmpRowRemoved.indexOf(currentRow) > 0)
{
//delete the row in the model
tableModel->removeRows(currentRow,1,tableView->currentIndex());
tmpRowRemoved.append(currentRow);
}
}
}
else{}
}
Can the delegate or the proxy cause a problem? Is there something to add in the model?
Please help. I'm searching for a long time where it bugs... But those MVCs are quite tough to debug...
Thx,
Xelag