August 12, 2011

RainWhisper RainWhisper
Lab Rat
5 posts

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 :(

7 replies

August 12, 2011

loladiro loladiro
Lab Rat
596 posts

Have a look at QWidget::closeEvent() [doc.qt.nokia.com]

August 15, 2011

Volker Volker
Robot Herder
5428 posts

In the close event you should add:

  1. QModelIndex index = currentIndex();
  2. currentChanged( index, index );

This commits the current edited cell to the model.

August 29, 2011

RainWhisper RainWhisper
Lab Rat
5 posts

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?

September 1, 2011

Volker Volker
Robot Herder
5428 posts

You can subclass the tree view, add a method (probably as slot) called endEdit() and put the two lines into it:

  1. class MyTreeView : public QTreeView
  2. {
  3. // ..
  4. public slots:
  5.     void endEdit();
  6. };
  7.  
  8.  
  9. void MyTreeView::endEdit()
  10. {
  11.     QModelIndex index = currentIndex();
  12.     currentChanged( index, index );
  13. }

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.

September 1, 2011

RainWhisper RainWhisper
Lab Rat
5 posts

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.

September 1, 2011

Eus Eus
Lab Rat
126 posts

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 [doc.qt.nokia.com] and MDI [doc.qt.nokia.com] application examples.

September 1, 2011

Volker Volker
Robot Herder
5428 posts

RainWhisper wrote:
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

  1. bool MyTableView:isEditing()
  2. {
  3.     return state() == QAbstractItemView::EditingState;
  4. }

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.

 
  ‹‹ Some issues in plugin design...      [Solved]Member variables don’t seem to exist inside class ››

You must log in to post a reply. Not a member yet? Register here!