How to remove items from c++ model inside qml?
-
@CoderJeff If you replace the code in
data()
with the following, it will work:QVariant MySqlModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); switch(role) { case Id: { return record(index.row()).value(0).toString(); } case Name : { return record(index.row()).value(1).toString(); } case Author : { return record(index.row()).value(2).toString(); } case Year : { return record(index.row()).value(3).toString(); } } return QSqlQueryModel::data(index, role); } ... setQuery(strQuery); var0 = record(1).value(0); var1 = record(0).value(1); qDebug() << var0 << var1;
Trying to find the reason.
-
That's what I am confused with. In addition, I tested the following statements, and they all works.
record(0).indexof(...)
record(0).index(...)
data(...)I do not understand why only record(0).value(...) can not work.
I tried to delete a chosen row, but it could not.I do not know whether it is the above reason. I have updated my code. Can you please check them? Thank you.
-
@p3c0 said:
@CoderJeff If you replace the code in
data()
with the following, it will work:QVariant MySqlModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); switch(role) { case Id: { return record(index.row()).value(0).toString(); } case Name : { return record(index.row()).value(1).toString(); } case Author : { return record(index.row()).value(2).toString(); } case Year : { return record(index.row()).value(3).toString(); } } return QSqlQueryModel::data(index, role); } ... setQuery(strQuery); var0 = record(1).value(0); var1 = record(0).value(1); qDebug() << var0 << var1;
Trying to find the reason.
Yes, it works. The value of val0 and val1 are both right this time. But I still can not delete a row.
-
@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 ? -
I used a temporary table to achieve my expected function. In addition, call setTable and select as the SqlTableModelTest did.
-
@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. -
Is a table not necessary when using a delete query?
-
@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
. -
Actually, I was wondering what you said for an evening, and still did not understand it. Can you show me an example?
If not duplicate the table first, after deleting a record from the table, how can I restore it?
-
@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.
-
That is not what I mean.
I just want to delete a row on GUI, not from the table. What you did will definitely delete a row from the table.
-
@CoderJeff Oh. Sorry, I thought its other way. Didn't understood what you meant here.