PDA

View Full Version : QTableView with QSqlTableModel - change widget for edit data



elektrrrus
9th August 2010, 16:52
Hi,

I have some piece of code which load data form single mysql table to table view. This data should be editable, so I`m using QSqlTableModel. But I want to change default widget to edit data, for example I have enum type in database with values "Yes" and "No" and for this item i want to have combobox widget with these values. Another example is field with possible integer values 0 and 1, here I want to have checkbox in table view. How to reach this? Maybe subclassing QTableView, but I have no idea how to do this.

pervlad
9th August 2010, 19:31
Inherit QSqlTableView add custom delegate that will display "Yes" "NO".That Delegate should return data type stored in db e.g. bool, char or int. As fare as I remember QSqlTableView there is example how to connect lookup tables to columns which results in automatically displayed combo-box on edit.

To understand model view programing in Qt read Model/View Programming (http://doc.trolltech.com/4.6/model-view-programming.html) and An Introduction to Model/View Programming (http://doc.qt.nokia.com/4.6/model-view-introduction.html).

I also believe that default edit delegate for bool type is check-box. If your model return bool for edit role check box should be displayed.

elektrrrus
9th August 2010, 21:05
Very thanks for aswer, it solve my problems.

fatecasino
24th February 2011, 16:51
Hi elektrrrus,

I am trying to do exactly the same but I am totally lost among tutorials, examples and documentation.
No solution so far. Could you please provide the code that created the model you described at the initial post?
Thanks in advance.