PDA

View Full Version : Updating SQL Database from QTableView



msweet
18th June 2010, 15:13
I am trying to update an SQL Database from a QTableView. I can view the contents of my database in my QTableView as well as insert a row into the QTableView and have it appear in my Database via manual submit and clicking a button. However I have not been able to edit a cell in my QTableView and have the change appear in the Database. Can someone point me in the right direction?

msweet
21st June 2010, 21:45
I still haven't been able to figure this out. I've been looking at the Cache Table example as a go by and what I'm trying to do isn't any different than what's in the example.

waynew
21st June 2010, 22:21
Sounds like it could be your edit strategy setting.
I have a model/view set on a sqlite database table with
model->setEditStrategy(QSqlTableModel::OnFieldChange);
If I double click in a view cell, make an edit, then hit return, the data change is saved to the database table.

msweet
22nd June 2010, 16:49
I just noticed something when trying what you suggested, when a cell is already populated and I make a change then it works. When a cell is blank from adding a row and not setting the data yet it doesn't work. So I'll play with it some more. I'm guessing by adding a row with blank cells and not setting the data the TableView doesn't know how to handle the edit.

msweet
22nd June 2010, 18:03
I figured it out. When adding rows (at least from my tests) you have to set data for all fields even if you initially want them blank to fill in later.

waynew
22nd June 2010, 22:18
Great - glad you got it working. I never had the problem since my rows are always populated when created.