[QTableView remove selected rows?
-
Hi guys, I have issue with QTableView... I can't remove selected items from the table correctly ... I got two methods:
@
for(int i=...)
{
view->model()->removeRow(i);
@Which works fine if I pick up each line separately, or this one:
@
for(int i=...)
{
view->model()->removeRow(view->currentIndex().row());
@
which works good only if I use Drag'n'Drop technique...Both works only in specific situation of selecting lines, but I can't reach the correct answer how to remove selected lines (by picking up, or by Drag'n'Drop)....How to remove lines from QTableView??
-
Maybe you need a "QTableView::selectedIndexes()":http://qt-project.org/doc/qt-4.8/qtableview.html#selectedIndexes.
-
Yea, I am using that one.
But I have solved that by decrementing (and removing from the end of the table view, not from the beginning):
I shouldn't increment over the list and remove the first items because table was shifting rows after I have removed one - and that was making issues. So I had to write decrementing cycle and removing from the end of the table view - after shifting I am able to remove the correct row.From:
@
QList<int> rowIndexes;
for (int i = 0; i < rowIndexes.count(); ++i)
tableView->model()->removeRow(rowsIndexes[i]); // shifting rows after removing causes "bad removing"
@To:
@
QList<int> rowIndexes;
for (int i = rowIndexes.count(); i >= 0; --i)
tableView->model()->removeRow(rowsIndexes[i]);
@ -
@Peppy
Hi All,
We can do using this push button method for deleting row of table.
void MainWindow::on_delete_record_clicked()
{QSqlQuery* query=new QSqlQuery(); QString str4; str4 = ui->lineEdit_5->text();
// int id=str4.toInt();
query->addBindValue(str4);
/*
QString str3;
str3 = ui->lineEdit_4->text();
query->addBindValue(str3);
*/
query->prepare("DELETE FROM entry where id = 'id' ");
// query->addBindValue(":id");
//ui->tablewidget->removeRow(ui->tablewidget->currentRow());query->exec(); if (query->exec()) { QMessageBox::critical(this,tr("Delete"),tr("Deleted")); //db.close(); }
-
// get the selected indexes QModelIndexList selectedIndexes = tableView->selectionModel()->selectedIndexes(); // make sure you are not deleting from a tree Q_ASSERT(std::equal(selectedIndexes.constBegin()+1,selectedIndexes.constEnd(),selectedIndexes.constBegin(),[](const QModelIndex& a,const QModelIndex& b)->bool{return a.parent()==b.parent();})); // sort from bottom to top std::sort( selectedIndexes.begin(),selectedIndexes.end(),[](const QModelIndex& a,const QModelIndex& b)->bool{return b.row()<a.row();}); QSet<int> deletedRows; // keep a record of rows already deleted for(auto i = selectedIndexes.constBegin();i!=selectedIndexes.constEnd();++i){ if(deletedRows.contains(i->row())) continue; //row deleted already, move on if(tableView->model()->removeRow(i->row(),i->parent())) // try to delete the row deletedRows << i->row(); // if successful store the row as deleted }
-
from http://doc.qt.io/qt-5/qsqlquerymodel.html:
The QSqlQueryModel class provides a read-only data model for SQL result sets.
Since it's read only it does not allow you to delete rows
-
QStandardItemModel + QSqlQuery https://forum.qt.io/topic/76135/qsqlrelationaltablemodel-and-complex-queries/21
QSqlTableModel http://doc.qt.io/qt-5/qsqltablemodel.html#details
-
QSqlTableModel or QSqlRelationalTableModel does that for you if you can live with simple queries. if you have convoluted joins then you'll have to connect to the model
rowsRemoved
orrowsAboutToBeRemoved
signal and prepare the query manually