QSqlQueryModel and TableView for dynamic data
-
Hi,
Just a side note, you're using QSqlDatabase wrongly. There's no need for that pointer.
See the class documentation for how to handle it.
-
wrote on 5 Jun 2018, 10:15 last edited by
If the db has enough elements to get the scrollbar in the table, then just scrolling the table the new elements are populated....so I suppose I'm missing any kind of notification so that the TableView is updated...just can't figure out how.
-
If the db has enough elements to get the scrollbar in the table, then just scrolling the table the new elements are populated....so I suppose I'm missing any kind of notification so that the TableView is updated...just can't figure out how.
-
@XDePedro
Not sure what you mean. Scrolling a table view does not fetch new elements via a new SQL query on the database....wrote on 5 Jun 2018, 10:36 last edited byIs not the table view fetching for new elements. If I just resize it just to make it redraw...the new elements are also displayed.
I am calling setQuery in the model everytime the database changes so I suppose the query is ok and the data is there....but just calling setQuery doesn't make the TableView to update.
How can I force the table view to update from the model?
-
Is not the table view fetching for new elements. If I just resize it just to make it redraw...the new elements are also displayed.
I am calling setQuery in the model everytime the database changes so I suppose the query is ok and the data is there....but just calling setQuery doesn't make the TableView to update.
How can I force the table view to update from the model?
wrote on 5 Jun 2018, 10:47 last edited by@XDePedro
For the update, see posts like:- http://www.qtcentre.org/threads/58700-How-to-update-QTableView-from-SQL-database-after-adding-data-to-database
- https://forum.qt.io/topic/1168/solved-the-best-way-to-programmatically-refresh-a-qsqlquerymodel-when-the-content-of-the-query-changes/13
- https://stackoverflow.com/questions/45359569/how-to-update-qtableview-on-qabstracttablemodel-change
However, I don't see how this addresses what I see as your issue, which is that you have "hundreds of thousands of rows" to display. You do not want to fetch all of them each time, you do not want to store them all in your model, nor in your view.... IMHO you only want to fetch some kind of "page" at a time of rows, or something along those lines.
-
wrote on 5 Jun 2018, 12:39 last edited by
I know maybe this not the right model but anyway I would like to make it work before looking for another solution.
I notice that I'm getting this error message:
QObject::connect: Cannot queue arguments of type 'QQmlChangeSet'
(Make sure 'QQmlChangeSet' is registered using qRegisterMetaType().)can it be the root cause? Does naybody know why I'm getting this? Might it be related with the fact that is another thread who is calling the setQuery??
-
wrote on 5 Jun 2018, 12:43 last edited by
-
wrote on 5 Jun 2018, 13:06 last edited by
Executing query in Main Thread after DB Changed
connect(populateWorker, &populateWorker::doneSignal, [this](){ QSqlQuery query(*database); database->open(); query->setQuery("SELECT * FROM Samples"); query->exec(); model->setQuery(query); database->close(); });
-
Executing query in Main Thread after DB Changed
connect(populateWorker, &populateWorker::doneSignal, [this](){ QSqlQuery query(*database); database->open(); query->setQuery("SELECT * FROM Samples"); query->exec(); model->setQuery(query); database->close(); });
wrote on 5 Jun 2018, 13:49 last edited by@KillerSmath
How is this different that I was doing?You added the slot in a lambda but is still executed in the working thread, as far as I know, Or I am missing anything.
I tried and seems not to work.
-
wrote on 5 Jun 2018, 15:00 last edited by
I am not expert with QThread but my suggest is create a class (DataManager) that will be the bridge between QML and C++ Model.
-
You can create a QThread inside this class to move the worker object to this thread.
-
Run your GenerateFakeData function from this worker and connect the doneSignal to setQuery Slot in DataManager
-
Allow the acess to model by this class
-
-
wrote on 6 Jun 2018, 07:59 last edited by
Ok. Now is working. It was a problem with the worker thread. I stopped the code in the code that updated the query (setQuery) in my previous implementation and was executed in the working thread. With current implementation the setQuery is executed in the UI thread.
In fact with current implementation the code is executed in one thread or the other just depending on how I made the signal/slot connection. So if I connect them this way:
connect(&insertWorker, SIGNAL(dbChanged()), this, SLOT(DBUpdated()));
void SampleListModel::DBUpdated() { QSqlQuery query;/ query.exec("SELECT * FROM Samples"); setQuery(query); }
Everything works and the setQuery is executed in the UI thread.
But if I do it this way:
connect( &insertWorker, &DBWorker::dbChanged, [this]() { QSqlQuery query(GetDataBase()); query.exec("SELECT * FROM Samples"); setQuery(query); });
Then the lambda code is executed in the worker thread and the real-time updates are not working.
As I said I am pretty new in Qt so I wonder whats the difference and if someone can recommend a good article explainning how the thread model works in Qt.
Thanks for all your help.
-
wrote on 6 Jun 2018, 08:11 last edited by
You welcome.
I also had difficulty with threads when i was starting to coding in QT but as time goes on, you'll understand why threads are very limited because of their unpredictability. -
Ok. Now is working. It was a problem with the worker thread. I stopped the code in the code that updated the query (setQuery) in my previous implementation and was executed in the working thread. With current implementation the setQuery is executed in the UI thread.
In fact with current implementation the code is executed in one thread or the other just depending on how I made the signal/slot connection. So if I connect them this way:
connect(&insertWorker, SIGNAL(dbChanged()), this, SLOT(DBUpdated()));
void SampleListModel::DBUpdated() { QSqlQuery query;/ query.exec("SELECT * FROM Samples"); setQuery(query); }
Everything works and the setQuery is executed in the UI thread.
But if I do it this way:
connect( &insertWorker, &DBWorker::dbChanged, [this]() { QSqlQuery query(GetDataBase()); query.exec("SELECT * FROM Samples"); setQuery(query); });
Then the lambda code is executed in the worker thread and the real-time updates are not working.
As I said I am pretty new in Qt so I wonder whats the difference and if someone can recommend a good article explainning how the thread model works in Qt.
Thanks for all your help.
wrote on 6 Jun 2018, 08:27 last edited by@XDePedro
I can't get my head around each of your two code approaches, but it's important to understand that all database objects/operations are constructed/performed in the same thread as each other, not in/from different threads. Is that the situation you are in now/does that explain anything not working? -
@XDePedro
I can't get my head around each of your two code approaches, but it's important to understand that all database objects/operations are constructed/performed in the same thread as each other, not in/from different threads. Is that the situation you are in now/does that explain anything not working? -
@JonB
Database insertions/write are done in the working thread.
Database queries/reads are done in the UI thread. -
wrote on 6 Jun 2018, 09:42 last edited by
@JonB
Seems to work, but I will not do it this way...teh database here is just a fake database in order to be able to work in the model while the real db is yet not implemented.Thanks.
Still wondering which model approach will feet better in what I ultimately have to implement. A lists of data that can have thousand of rows and can be updated by another thread (not user interaction). Any suggestions.
-
@JonB
Seems to work, but I will not do it this way...teh database here is just a fake database in order to be able to work in the model while the real db is yet not implemented.Thanks.
Still wondering which model approach will feet better in what I ultimately have to implement. A lists of data that can have thousand of rows and can be updated by another thread (not user interaction). Any suggestions.
wrote on 6 Jun 2018, 09:47 last edited by JonB 6 Jun 2018, 09:51@XDePedro
For the "updated by another thread (not user interaction)", my understanding is that you must not do that directly. If you need to, use signals & slots. You may get away without now, but supposedly not in the long run, under whatever circumstances.Read up on this, e.g. https://stackoverflow.com/questions/20793689/correctly-using-qsqldatabase-in-multi-threaded-programs, and many others.
For the "model approach", I still think you need to answer my earlier question: do you fetch a brand new set of rows and those are the only ones to be shown in the table view, or do you "incrementally" fetch some additional new rows, and want those to be appended to what you already have in the model & the view?
-
wrote on 6 Jun 2018, 12:24 last edited by
Any help about which model should I use? Any help on how implement it?
16/24