How to commit data being edited in QTableView on window close?
Could you help me to make QTableVew to commit changed before window will be closed by ‘X’ button or any by other means? It seems that QTableView commits only when editor widget loses the focus. If so, why the focus wouldn’t be lost after I clicked ‘X’ button of the window? I’m completely confused :(
In the close event you should add:
- QModelIndex index = currentIndex();
- currentChanged( index, index );
This commits the current edited cell to the model.
If I understand correctly QWidget::closeEvent is sent only to top-level widget. But I use this function to check if the model was modified and to ask user to save data if so. The model emits signal when data is changed. I connect this signal to a slot of main window class and this slot modifies the flag. So my question is: how can I have this flag been modified when QWidget::closeEvent method f main window class is called?
You can subclass the tree view, add a method (probably as slot) called endEdit() and put the two lines into it:
This is how we have done it in our project. You can call endEdit from your event handler then. It should update the flag – I have not tested the latter, though.
bq. You can call endEdit from your event handler then.
This will not help me because I check the flag straightforward in the same event handler.
Then call endEdit before you check the flag.
Or add a public method
- bool MyTableView:isEditing()
- return state() == QAbstractItemView::EditingState;
If it returns true, you are editing the cell, and thus committing the change to the model will have the model be modified.
Another alternative is to save the modified state in the model and check that before your ask-back code.