PDA

View Full Version : Update active editor in delegate when value underneath changes



RThaden
1st February 2008, 17:36
Hi all,

I have a QTableView with a custom model underneath and delegates to render the view in different ways.
If I e.g. change a double value, I use a QDoubleSpinBox which works fine.
However, it is possible that the value that is currently edited is changing due to another mechanism. Then, the model is informed of the change and emits dataChanged(). But then, the value in the already open spinbox does not change.

How do I inform the editor (QDoubleSpinBox) that it should update its value while it is active?
When I close the editor and change to another cell, the value is correctly updated but not _while_ I am editing it.

Any help appreciated.

Regards,

Rainer

wysota
1st February 2008, 19:14
You can connect to the dataChanged() signal, check if there is an editor opened for one of the modified indexes and call QAbstractItemDelegate::setEditorData().

RThaden
5th February 2008, 14:58
You can connect to the dataChanged() signal, check if there is an editor opened for one of the modified indexes and call QAbstractItemDelegate::setEditorData().

Thanks for your help.
That's what I was thinking, too. I just didn't know how to check for an opened editor.
This is what you have to do:
Connect the dataChanged signal to a custom slot updateEditor.
In your custom model, you need a pointer to the view, called tview here.
Then, in customEditor, you can call indexWidget on the view which returns the editor widget. Here a sample code:



void MyDelegate::updateEditor(const QModelIndex& start, const QModelIndex& end)
{
QWidget* widget = tview->indexWidget(start);

if (widget)
{
switch(start.column())
{
// edit only these columns, leave out others
case col1:
case col2:
case col4:
{
setEditorData(widget, start);
break;
}
default:
break;
}
}
}


Of course, you have to do some more necessary checks.
If there is an easier way, please let me know.

Regards,

Rainer