5 Attachment(s)
How to update QTableView from SQL database after adding data to database?
I have 2 classes, one is a mainwindow (iherits from QMainWindow), and second is addwindow (iherits from QDialog). In mainwindow I have QTableView connected with database, and buttons like "add data", "remove data". In addwindow I put data and click "ok" button to add data to database. Now, how could I call "tablica->select()" to update QTableView after clicked "ok" button in addwindow?
Problem is that QSqlRelationalTableModel associated with QTableView is included in mainwindow constructor, and method adddata is included in addwindow class.
There are my header and c++ files.
Attachment 10264Attachment 10265Attachment 10266Attachment 10267Attachment 10268
EDIT: I just found after hours, that thing I supposed to do was add connection in clicking "add data" slot in mainwindow class.
Re: How to update QTableView from SQL database after adding data to database?
If you do the addition or modification through the same model (with addRows(), removeRows() or setData()), as the view is looking at then then the updates become available to the view. If you, or something else, updates the underlying database directly then you have no choice but to requery the database.
Re: How to update QTableView from SQL database after adding data to database?
Or set table model with QSqlQueryModel and after adding data call a slot which contains model->setQuery(<your query>) which will update the view.