Hi there guys,
I have a bit of a problem here I have a tiny problem here. I have a database connected to a QSqlTableModel, which in turn is connected to a QSortFilterProxyModel which in turn is connected to a QTableView. I also have the following code for deleting records:
void mainwindow::deletedialog() {
button
= QMessageBox::question(this, tr
("Delete Record"),
QString (tr
("Are you sure you wish to delete this record?")),
//Declaring an item selection which we will use to see which row the user has selected
QList<int> rows;
foreach
( const QModelIndex & index, selection.
indexes() ) { rows.append( index.row() );
}
qSort( rows );
int prev = -1;
for( int i = rows.count() - 1; i >= 0; i -= 1 ) {
int current = rows[i];
if( current != prev ) {
model->removeRows( current, 1 );
prev = current;
}
}
}
else {
QMessageBox::information(this, tr
("Delete Record"), tr
("Please select the record you wish to delete"));
}
}
void mainwindow::deletedialog() {
QMessageBox::StandardButton button;
button = QMessageBox::question(this, tr("Delete Record"),
QString (tr("Are you sure you wish to delete this record?")),
QMessageBox::Yes | QMessageBox::No);
if (button == QMessageBox::Yes) {
//Declaring an item selection which we will use to see which row the user has selected
QItemSelection selection = ui->View->selectionModel()->selection();
QList<int> rows;
foreach ( const QModelIndex & index, selection.indexes() ) {
rows.append( index.row() );
}
qSort( rows );
int prev = -1;
for( int i = rows.count() - 1; i >= 0; i -= 1 ) {
int current = rows[i];
if( current != prev ) {
model->removeRows( current, 1 );
prev = current;
}
}
}
else {
QMessageBox::information(this, tr("Delete Record"), tr("Please select the record you wish to delete"));
}
}
To copy to clipboard, switch view to plain text mode
This code works fine unless we filter the contents of the table. If we have say 600 rows i the table, and we filter everything until we obtain 1 single record, then the code above may not delete the correct record. How can I make the above code work with QSortFilterProxyModel?
Any sample code would be greatly appreciated.
Thanks in advance,
Nefastious
Bookmarks