How to remove items from c++ model inside qml?
-
@CoderJeff That is because
QSqlquerymodel
is a read-only model. See here. You will need to delete the row using another query and then update the view. -
I tried to use QSqlTableModel, and it also did not work.
Here I do not want to use another query. I just want to delete a row from the model instead of the table. In other words, only on GUI it seems to be deleted, but it is still in the table actually.
-
@CoderJeff Sorry I too donot know about this. This QTBUG-37538 seems to be similar but using
ListView
.
Test it withQTableView
to see if it works ? -
@CoderJeff Ok, but then why not execute a delete query instead of creating another temporary table ? I guess
removeRow
forQSqlTableModel
too does the same internally. -
@CoderJeff table is required ofcourse but why duplicate it first. What you require can be achieved by firing another delete query and then reloading the model so that it reflects in
TableView
. -
@CoderJeff Since you only want to delete a row it can be done as follows:
//MySqlModel.cpp void MySqlModel::removeMyRow(int r) { QString strQuery("DELETE FROM Book where id="+QString::number(r)); QSqlQuery sq; sq.prepare(strQuery); sq.exec(); strQuery = "SELECT * FROM Book"; sq.prepare(strQuery); sq.exec(); setQuery(sq); }
This will delete the row first and then reload the model with updated data.
-
@CoderJeff Oh. Sorry, I thought its other way. Didn't understood what you meant here.