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 can I get previous data of QTableWidgetItem?
Forum Updated to NodeBB v4.3 + New Features

How can I get previous data of QTableWidgetItem?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 2.3k 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.
  • A Offline
    A Offline
    Aaron Kim
    wrote on last edited by
    #1

    itemChanged(QTableWidgetItem * item) signal can not retrieve previous(before change) data.
    I can't find other signals which suit this case.

    JonBJ Taz742T 2 Replies Last reply
    0
    • A Aaron Kim

      itemChanged(QTableWidgetItem * item) signal can not retrieve previous(before change) data.
      I can't find other signals which suit this case.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Aaron-Kim
      Since there does not appear to be some beforeItemChanged() signal, unless you know just what will cause the item changed in your code (e.g. can you look at the model's setData() perhaps?) you are on your own. You would have to have stored up the data's previous value yourself somewhere. I don't think you're supposed to try to get back at old data from the view, I think you're supposed to do it from the model side.

      1 Reply Last reply
      0
      • A Aaron Kim

        itemChanged(QTableWidgetItem * item) signal can not retrieve previous(before change) data.
        I can't find other signals which suit this case.

        Taz742T Offline
        Taz742T Offline
        Taz742
        wrote on last edited by
        #3

        @Aaron-Kim
        You need to save it somewhere.

        Something like this:
        .h file

        private:
            vector<vector<QString> > arr;
        

        .cpp constructor:

            for (int i = 0; i < 10; i++) {
                vector<QString> t;
                for (int j = 0; j < 5; j++) {
                    t.push_back(QString("%1, %2").arg(QString::number(i), QString::number(j)));
                }
                arr.push_back(t);
            }
        
            ui->tableWidget->setColumnCount(5);
            ui->tableWidget->setRowCount(arr.size());
        
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 5; j++) {
                    ui->tableWidget->setItem(i, j, new QTableWidgetItem(arr[i][j]));
                }
            }
        
            connect(ui->tableWidget, &QTableWidget::itemChanged, this, [=](QTableWidgetItem *item) {
                int row = item->row();
                int column = item->column();
                qDebug() << "previous value: " << this->arr[row][column];
                qDebug() << "new value: " << item->text();
                this->arr[row][column] = item->text();
            });
        

        Do what you want.

        1 Reply Last reply
        1
        • A Offline
          A Offline
          Aaron Kim
          wrote on last edited by
          #4

          Thanks. It seems that it is the only way.

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

            Requires Qt >= 5.11

            QObject::connect(tableWidget->model(),&QAbstractItemModel::dataChanged,[=](const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles){
                if(!roles.isEmpty() && !roles.contains(Qt::EditRole))
                    return;
                for(int i=topLeft.row();i<=bottomRight.row();++i){
                    for(int j=topLeft.column();j<=bottomRight.column();++j){
                        const QModelIndex currIdx = topLeft.model()->index(i,j,topLeft.parent());
                        qDebug() << i << ";" << j << " Previous Value: " << currIdx.data(Qt::UserRole);
                        tableWidget->model()->setData(currIdx, currIdx.data(Qt::EditRole), Qt::UserRole);
                    }
                }
            });

            "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
            4

            • Login

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