[QTableView remove selected rows?
-
// 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 -
... it's included as part of Qt's examples: http://doc.qt.io/qt-5/qtsql-relationaltablemodel-example.html
-
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