[QTableView remove selected rows?
-
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]);
@wrote on 1 Mar 2017, 07:31 last edited by@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(); }
-
wrote on 10 Apr 2017, 07:29 last edited by
Hi All,
With the above code i could not able to remove the selected rows from the QTableview is interface with database conncetion......please any one guide me -
wrote on 2 May 2017, 10:07 last edited by
I am not able to delete by selecting entire row from the Qtableview using delete push button ...give me some examples
-
wrote on 2 May 2017, 10:37 last edited by VRonin 5 Feb 2017, 14:06
// 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 }
-
// 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 }
-
I got selectedIndexes is 5 ITEMS while debugging my code "deletedRows" is 0 after that my selected row is remains same only it is not deleted .....my QTableview is connected with database.....
wrote on 2 May 2017, 13:06 last edited by@veera said in [QTableView remove selected rows?:
QTableview is connected with database
What model are you using?
-
@veera said in [QTableView remove selected rows?:
QTableview is connected with database
What model are you using?
-
wrote on 2 May 2017, 13:45 last edited by VRonin 5 Feb 2017, 13:52
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
-
wrote on 2 May 2017, 13:56 last edited by
Then which model i need to use and how to do it please give me some examples ........
-
wrote on 2 May 2017, 14:05 last edited by
either QSqlTableModel (be carefull with this one, read the docs) or you manually populate a QStandardItemModel from the data you get from a QSqlQuery
-
wrote on 2 May 2017, 14:36 last edited by
give me some examples so that i can understand it better
-
wrote on 2 May 2017, 14:42 last edited by VRonin 5 Feb 2017, 14:43
QStandardItemModel + QSqlQuery https://forum.qt.io/topic/76135/qsqlrelationaltablemodel-and-complex-queries/21
QSqlTableModel http://doc.qt.io/qt-5/qsqltablemodel.html#details
-
wrote on 2 May 2017, 15:11 last edited by
Its not happening ....... i have selected one and tried but its not deleting and tried many rows also not deleting my database is MSSQL in Ubuntu.....
-
wrote on 2 May 2017, 15:30 last edited by
30 minutes before giving up?
create a dummy table in your database (so even in case of a disaster you won't lose anything important) and try with QSqlTableModel
-
wrote on 3 May 2017, 05:41 last edited by
I want to something this type of query
query->prepare("DELETE FROM Info WHERE ID = ?");
query->addBindValue(ID);
for deleting of entire row by selecting a particular row........... -
wrote on 3 May 2017, 07:12 last edited by
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 -
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 -
wrote on 3 May 2017, 07:34 last edited by
... it's included as part of Qt's examples: http://doc.qt.io/qt-5/qtsql-relationaltablemodel-example.html
-
wrote on 3 May 2017, 09:07 last edited by
i go through the examples in that they creating QTableView *view = new QTableView; but i am designed in the UI drag and drop of Tableview and everything so.........
-
wrote on 3 May 2017, 09:19 last edited by VRonin 5 Mar 2017, 09:19
There is absolutely no difference. The trag and drop in Qt Designer generates an xml file describing your interface then
uic
converts that xml into code basically identical to the one in the examples.On top of this, your problem is not in the UI but in the model design so you should not really care about how the UI is created