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. QTableview editing by row
Forum Updated to NodeBB v4.3 + New Features

QTableview editing by row

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtableviewediting by row
6 Posts 2 Posters 4.6k Views
  • 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.
  • B Offline
    B Offline
    blastdoman
    wrote on last edited by
    #1

    Hello partners.

    I have experience with QStandardItemModel where the user is entering data in the cells using delegates and are updated directly in the model for later, with a "Save" button the data entered in the database is updated.

    What I want to do now is something different, when the user changes line (by means of Return, pressing outside the line that is being edited or because Tab is pressed in the last column of the row) a process of verification of the introduced data. If everything is fine, the database should be updated and if not, a message should be displayed with the errors.

    Looking for information I've seen things with QAbstractItemModel, the slots submit and revert but I do not completely understand it. For example, I do not understand how you are told to check data only when you switch lines.

    Could someone help me? A little example would be nice.

    Thank you very much.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Declare a private int currentRow= -1;
      then

      connect(tableView->selectionModel(),&QItemSelectionModel::currentChanged,this,[this](const QModelIndex& idx)->void{
      const int newRow= idx.isValid() ? idx.row():-1;
      if(newRow==currentRow) return;
      if(currentRow>=0)
      validateRow(currentRow);
      currentRow=newRow;
      });
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • B Offline
        B Offline
        blastdoman
        wrote on last edited by
        #3

        That's not what I need. One thing is that the user can select the rows, in fact, I want it to be able to do it; and another is that when leaving the row editing mode using one of the three forms, make validation of the data in the row and if this has gone well, update the database. Any advice?

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Subclass the delegate:

          #include <QStyledItemDelegate>
          class DataChangeDelegate : public QStyledItemDelegate{
              Q_OBJECT
              Q_DISABLE_COPY(DataChangeDelegate)
          public:
              explicit DataChangeDelegate(QObject* parent=Q_NULLPTR)
                  :QStyledItemDelegate(parent)
              {}
              virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE{
                  const QVariant oldData = index.data(Qt::EditRole);
                  QStyledItemDelegate::setModelData(editor,model,index);
                  if(index.data(Qt::EditRole)!=oldData)
                      dataChanged(index);
              }
              Q_SIGNAL void dataChanged(const QModelIndex &index);
          };
          

          then use:

          DataChangeDelegate* signalingDelegate=new DataChangeDelegate(this);
          connect(signalingDelegate,&DataChangeDelegate::dataChanged,this,[this](const QModelIndex& idx)->void{
          const int newRow= idx.isValid() ? idx.row():-1;
          if(newRow==currentRow) return;
          if(currentRow>=0)
          validateRow(currentRow);
          currentRow=newRow;
          });
          tableView->setItemDelegate(signalingDelegate);
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • B Offline
            B Offline
            blastdoman
            wrote on last edited by
            #5

            I think that solution is not correct either. If the user is editing a row, it will not be validated until you change a data in another row, that is, if the user changes the value of column A of row 5 and click on row 7, with this solution row 5 would not be valid until a data item in row 7 was changed, which may or may not be the case. It is not a valid solution.

            Anyway thanks for the interest :-)

            VRoninV 1 Reply Last reply
            0
            • B blastdoman

              I think that solution is not correct either. If the user is editing a row, it will not be validated until you change a data in another row, that is, if the user changes the value of column A of row 5 and click on row 7, with this solution row 5 would not be valid until a data item in row 7 was changed, which may or may not be the case. It is not a valid solution.

              Anyway thanks for the interest :-)

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @blastdoman You are correct, let's try this:

              #include <QStyledItemDelegate>
              class DataChangeDelegate : public QStyledItemDelegate{
                  Q_OBJECT
                  Q_DISABLE_COPY(DataChangeDelegate)
              public:
              enum{IsDirtyRole=Qt::UserRole + 44};
                  explicit DataChangeDelegate(QObject* parent=Q_NULLPTR)
                      :QStyledItemDelegate(parent)
                  {}
                  virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE{
                      const QVariant oldData = index.data(Qt::EditRole);
                      QStyledItemDelegate::setModelData(editor,model,index);
                      if(index.data(Qt::EditRole)!=oldData)
                          model->setData(index,true,IsDirtyRole);
                  }
              };
              
              DataChangeDelegate* signalingDelegate=new DataChangeDelegate(this);
              connect(tableView->selectionModel(),&QItemSelectionModel::currentChanged,this,[this](const QModelIndex& idx)->void{
              const int newRow= idx.isValid() ? idx.row():-1;
              if(newRow==currentRow) return;
              if(currentRow>=0){
              const int maxCols =idx.model()->columnCount(idx.parent());
              for(int i=0;i<maxCols ;++i){
              if(idx.model()->index(currentRow,i,idx.parent()).data(DataChangeDelegate::IsDirtyRole).toBool()){
              validateRow(currentRow); 
              /*
              inside validateRow call something like:
              for(int i=0;i<model->columnCount();++i)
              model->setData(model->index(currentRow,i),QVariant(),DataChangeDelegate::IsDirtyRole);
              */
              break;
              }
              }
              }
              currentRow=newRow;
              });
              tableView->setItemDelegate(signalingDelegate);
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2

              • Login

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