PDA

View Full Version : How to update QTableView from SQL database after adding data to database?



Voonter
5th April 2014, 17:39
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.

1026410265102661026710268

EDIT: I just found after hours, that thing I supposed to do was add connection in clicking "add data" slot in mainwindow class.

ChrisW67
5th April 2014, 21:44
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.

adutzu89
6th April 2014, 11:07
Or set table model with QSqlQueryModel and after adding data call a slot which contains model->setQuery(<your query>) which will update the view.