QSqlRelationalTableModel and complex queries
-
The problem is I don't have the slightest idea how to do all this.
Is there some example I could look at?EDIT: QStandardItemModel has no setQuery function.
How shall I "fill it with a QSqlQuery"?
You mean wit QStandardItemModel::setItem?wrote on 14 Feb 2017, 17:12 last edited by VRonin@Panoss said in QSqlRelationalTableModel and complex queries:
How shall I "fill it with a QSqlQuery"
manually, outside the model
model->removeRows(0, model->rowCount()); model->removeColumns(0, model->columnCount()); QSqlQuery testQuery; testQuery.prepare("select * from MyTable"); if (testQuery.exec()) { for (bool firstRun = true; testQuery.next();) { const QSqlRecord currRecord = testQuery.record(); if (firstRun) { firstRun = false; model->insertColumns(0, currRecord.count()); for (int i = 0; i < currRecord.count(); ++i) model->setHeaderData(i, Qt::Horizontal, currRecord.fieldName(i)); } const int newRow = model->rowCount(); model->insertRow(newRow); for (int i = 0; i < currRecord.count(); ++i) model->setData(model->index(newRow, i), currRecord.value(i)); } }
-
wrote on 14 Feb 2017, 17:52 last edited by Panoss
Works, the model->removeRow works, it removes a row from the model ,but how do I remove this row from the db too? Something like this?
QSqlQuery query; query.prepare("DELETE FROM MyTable WHERE id=1"); if (query.exec()) { etc. etc.
-
You'll likely have to make a more precise delete query but otherwise, yes.
-
wrote on 14 Feb 2017, 21:12 last edited by
What do you mean by "more precise"? I thought "WHERE id=1" makes it absolutely precise (the value "1" was chosen for the shake of the example).
-
Sorry, that was badly written, I meant that you would have to ensure that you are passing the correct parameter(s) to the delete query to ensure you are deleting the same row in the database.
-
wrote on 14 Feb 2017, 21:23 last edited by
Ah, ok, guys, VRonin, SGaist, thank you very much.
-
wrote on 15 Feb 2017, 09:28 last edited by VRonin
be careful here as if you naively connect the database delete to
rowsRemoved
the first two lines:
(model->removeRows(0, model->rowCount()); model->removeColumns(0, model->columnCount());
)
will delete your entire database and you cannot just use aQSignalBlocker
on the model either or the view won't update -
I was using QSqlRelationalTableModel for making the model of a QTableView.
But I had to had some complex queries (LEFT JOINS, plus concatenating fields) (can I concatenate fields with QSqlRelationalTableModel? I think not).
But, QSqlRelationalTableModel cannot accept queries (the ->setQuery is private), only the ->setTable() works.
So I replaced QSqlRelationalTableModel with QSqlQueryModel.Is this the best approach or am I missing something?
wrote on 4 Jun 2022, 22:18 last edited by kkmspb 6 Apr 2022, 22:23@Panoss Hi! I solved this problem. You can extended Qt sources. First you change QSqlRelationalTableModel in QSqlTabelModel style, i.e. you create qsqlrelationaltabelmodel_p.h file. Afterwards you can derive from QSqlRelationalTableModel and you wil get access to private data.
Than you rebuild qt souces.
Only you needs reimplement selectStatement.
(Sorry for my english). I'v done this on qt 4.8.1.