How to detect QTableWidget cell editing finished
-
Re: [how to edit one item in QTableWidget](and how to call my function when Edit finished(eg.press Enter)
"You can derive from QTableWidgetItem and override setData() which is called when data changes (e.g. when the editing finished)"
Is this still really the only way to achieve this? What about if the user does not change the data? Just enters and exits the editing mode. Will the setData still be called?
I have a cell that gets a new value periodically. I would like to run a method when entering and exiting the editing mode.
-
@Jari-Koivuluoma said in how to detect QTableWidget cell editing finished:
Just enters and exits the editing mode. Will the setData still be called?
No, it will not.
I have a cell that gets a new value periodically. I would like to run a method when entering and exiting the editing mode.
I don't know what you have in mind here or why it affects anything.
But anyway, if you do want to know when "editing has finished by whatever means": Qt would not normally tell you about exit when nothing happened. If you do want that I think you should try attaching a slot to signal
void QAbstractItemDelegate::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint = NoHint).
You obtain theQAbstractItemDelegate
from aQTableWidget
via
QAbstractItemDelegate *QAbstractItemView::itemDelegate() constExample (using old Qt4 connection syntax, you should use new style) is at https://stackoverflow.com/a/33080359, where the poster states
All another signals from models will not work if user does not change anything in cell, but delegate is closed everytime when editing is finished.
-
To clarify the situation. I have a column where the cells are showing analog output values from a automation system PLC device (0-100% of the output range). Those values are received and refreshed trough a network periodically. The idea is to send a replacement value to the PLC by changing the cell value.
I would do this by first disabling the cell value from updating while Im editing it. I would do that in a method running at the beginning of the edit. Now using the cellDoubleClicked signal for that and its seems to work fine.
After the edit (focus lost, enter pressed or what not), I want to send the new value to the PLC and enable the refreshing of the cell again. Now Im using the itemChanged signal and I cant re-enable the refreshing after the edit if the value has not been changed.
Also I would like to send the new value once the editing is finished, not at every keypress.
Edit:
I tried the following and for now it seems to work.
I used the cellDoubleClicked signal to set a boolean which prevents the cell value update and also save the edited cell row to a variable.
Then I did connect(ui->twAIOs->itemDelegate(),SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),SLOT(aioItemChanged()));
In the aioItemChanged method I used the saved row to fetch and send the new value to PLC and re-enable the refresing of the cell(column is fixed).
-
@Jari-Koivuluoma said in How to detect QTableWidget cell editing finished:
Then I did
connect(ui->twAIOs->itemDelegate(),SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),SLOT(aioItemChanged()));
Indeed that is correct. It's just a "shame" you did not choose to update that to new style syntax, but up to you :)