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 perform action before and after user edits element in QTableView?
Forum Updated to NodeBB v4.3 + New Features

How to perform action before and after user edits element in QTableView?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 472 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
    Rodrigo B.
    wrote on last edited by
    #1

    I have a QTableView and would like to perform certain actions before and after the user edits certain elements in the view.

    At first I naively introduced the code for these actions in my model's setData model, but then the actions are performed in other circumstances, for example when a row is dragged and dropped (since that invokes setData as well). I am not sure there is a way for me to tell when setData is being invoked as a result of manual editing, and when it is invoked because of other events.

    So, in summary, how can I insert code that is run only before and after the user edits an element in a QTableView? Thanks.

    eyllanescE 1 Reply Last reply
    0
    • R Rodrigo B.

      I have a QTableView and would like to perform certain actions before and after the user edits certain elements in the view.

      At first I naively introduced the code for these actions in my model's setData model, but then the actions are performed in other circumstances, for example when a row is dragged and dropped (since that invokes setData as well). I am not sure there is a way for me to tell when setData is being invoked as a result of manual editing, and when it is invoked because of other events.

      So, in summary, how can I insert code that is run only before and after the user edits an element in a QTableView? Thanks.

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

      @Rodrigo-B Use a delegate:

      #include <QApplication>
      #include <QStandardItemModel>
      #include <QStyledItemDelegate>
      #include <QTableView>
      
      #include <QDebug>
      
      class Delegate: public QStyledItemDelegate{
      public:
          using QStyledItemDelegate::QStyledItemDelegate;
          void setEditorData(QWidget *editor, const QModelIndex &index) const{
              if(editor->isHidden()){
                  qDebug() << "before";
              }
              QStyledItemDelegate::setEditorData(editor, index);
          }
          void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
              QStyledItemDelegate::setModelData(editor, model, index);
              qDebug() << "after";
          }
      };
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QTableView w;
          w.setItemDelegate(new Delegate(&w));
          QStandardItemModel model(4, 4);
          w.setModel(&model);
          w.show();
          return a.exec();
      }
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      4
      • R Offline
        R Offline
        Rodrigo B.
        wrote on last edited by
        #3

        Great, thank you!

        Can you please tell me why you need to check editor->isHidden()?

        eyllanescE 1 Reply Last reply
        0
        • R Rodrigo B.

          Great, thank you!

          Can you please tell me why you need to check editor->isHidden()?

          eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by
          #4

          @Rodrigo-B When testing my code I verified that setEditorData was called 2 times so to avoid it use that condition.

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          R 1 Reply Last reply
          0
          • eyllanescE eyllanesc

            @Rodrigo-B When testing my code I verified that setEditorData was called 2 times so to avoid it use that condition.

            R Offline
            R Offline
            Rodrigo B.
            wrote on last edited by
            #5

            @eyllanesc Thank you!

            Does anyone know why this is called twice? It seems like it should be called only once.

            eyllanescE 1 Reply Last reply
            0
            • R Rodrigo B.

              @eyllanesc Thank you!

              Does anyone know why this is called twice? It seems like it should be called only once.

              eyllanescE Offline
              eyllanescE Offline
              eyllanesc
              wrote on last edited by
              #6

              @Rodrigo-B I would have to check the source code of the class but I am lazy to do so, if you want to understand the cause or check the source code or debug Qt.

              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

              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