PDA

View Full Version : How to know when field is edited within QTableView



dkite
29th December 2006, 15:18
I have a table view within a window with many other widgets, and would like to disable other widgets if a field is edited within the table view. I would like to enable a commit button also.


sourcemodel = new QSqlRelationalTableModel();
sourcemodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
model = new jobItemSortFilterProxyModel();
model->setSourceModel(sourcemodel);
sourcemodel->setTable("jobitem");
sourcemodel->select();

Pretty straight forward. How can I pick up when the various delegates, ie. text edit or spin box change the data?

The dataChanged() signal fires when setData is called, not when editing begins.

Derek

jpn
29th December 2006, 16:00
Create a custom delegate and emit signals from:

QAbstractItemDelegate::setEditorData() (edit begins)
QAbstractItemDelegate::setModelData() (edit ends)


You can easily achieve this by subclassing the default delegate implementation QItemDelegate, adding a couple of signals, overriding methods listed above and emitting corresponding signals from there.



void MyItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
QItemDelegate::setEditorData(editor, index);
emit editBegins();
}

void MyItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
QItemDelegate::setModelData(editor, model, index);
emit editEnds();
}

wysota
29th December 2006, 16:18
For simple uses you might just connect to the activated() signal of the view, but be warned, that it'll not handle all possible situations. It depends which edit triggers you use.

dkite
1st January 2007, 18:51
For completeness, emitting a signal from a const method:


void MyItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
QItemDelegate::setEditorData(editor, index);
emit const_cast<MyItemDelegate*>(this)->editBegins();
}

http://lists.trolltech.com/qt-interest/2005-09/thread01411-0.html