PDA

View Full Version : Value changed in a QTableView field



nittalope
9th August 2009, 21:30
I've searched a bit, but could not found how to do this.

Have got a QSqlTableModel and a QTableView which I use to show the data.
I would like to know if, when I change the value of a field in QTableView, a signal is emitted, because I need to call a function when the user edit a field.

Don't care, now, to change the data directly in database, I'll do the commit manually.

john_god
12th August 2009, 00:49
I never used QSqlTableModel, but as far as QTableView if you want users to edit data I think you should use QTableWidget instead. Assuming you're using QTCreator just right clik on QTableWidget choose 'Go to Slot... ' option, and a list with Signal will apear. I think QTableWidget::itemChanged ( QTableWidgetItem * item ) will do.

wysota
12th August 2009, 01:05
For every model the QAbstractItemModel::dataChanged() signal is emitted when contents of the model change. So simply go through QAbstractItemView::model() to find the appropriate signal and connect to it.

franz
12th August 2009, 07:47
I never used QSqlTableModel, but as far as QTableView if you want users to edit data I think you should use QTableWidget instead.

Just a remark here: QTableWidget is basically a QTableModel a filter and a QTableView put into one convenience widget. When the model supports data for the Qt::EditRole, then QTableView will be perfectly able to support editing of data.

nittalope
12th August 2009, 09:21
@wysota:
thank you! that was what I was looking for!

@john_god:
franz is right, no problem at all editing data in QTableView!

By the way here is how I solved it:



myModel = new QSqlRelationalTableModel(this);

connect(myModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(myFunction()));


and then I implemented myFunction() to do what I need to do ;)