How to commit data being edited in QTableView on window close?
-
Hi, everyone!
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 :(
-
Have a look at "QWidget::closeEvent()":http://doc.qt.nokia.com/4.8-snapshot/qwidget.html#closeEvent
-
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:
@
class MyTreeView : public QTreeView
{
// ..
public slots:
void endEdit();
};void MyTreeView::endEdit()
{
QModelIndex index = currentIndex();
currentChanged( index, index );
}
@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.
-
as loladiro suggested, check QWidget::closeEvent()
You can add your function in there which will save the data.
You can refer to the docs and the examples available there! For example the "SDI":http://doc.qt.nokia.com/latest/mainwindows-sdi.html and "MDI":http://doc.qt.nokia.com/latest/mainwindows-mdi.html application examples. -
[quote author="RainWhisper" date="1314858814"]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.[/quote]
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.