Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to commit data being edited in QTableView on window close?
Forum Updated to NodeBB v4.3 + New Features

How to commit data being edited in QTableView on window close?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 8.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RainWhisper
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

      Have a look at "QWidget::closeEvent()":http://doc.qt.nokia.com/4.8-snapshot/qwidget.html#closeEvent

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        In the close event you should add:

        @
        QModelIndex index = currentIndex();
        currentChanged( index, index );
        @

        This commits the current edited cell to the model.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • R Offline
          R Offline
          RainWhisper
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • R Offline
              R Offline
              RainWhisper
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Robbin
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  [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.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved