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 is erasing cell content when I edit it
Forum Updated to NodeBB v4.3 + New Features

QTableView is erasing cell content when I edit it

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.2k 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.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by
    #1

    I've added a QTableView to a form and extended a QTableModel to handle the data that should be stored within the table. One of the columns contains string data and should be editable by the user. I've set things up so that the user can click a cell in the table, which causes an editor textbox to appear allowing the user to type in the content for the cell. However, when this editor box opens, it ignores any content already in the cell.

    Even if my cell has a word like 'orange' in it, when the user enters edit mode to change the value of the cell, the edit text box is empty. I would like the editor box to contain a copy of the cell's data as it currently exists in the table so the user can edit it rather than having to recreate it. Is there a way to do this?

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Then your TableModel does not return anything for Qt::EditRole.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • K Offline
        K Offline
        kitfox
        wrote on last edited by
        #3

        I'm not sure what you mean. Qt::EditRole is a constant, not a method. I am checking for Qt::EditRole in setData() and it allows me to set the data properly, but is not called when the cell is preparing to be edited.

        
        bool DopeTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
        {
            if (role == Qt::EditRole)
            {
                const DopeRow& row = _rows[index.row()];
                DopeRow newRow = row;
        
                switch (index.column())
                {
                case 2:
                    newRow.phoneme = value.toString();
                    break;
                case 3:
                    newRow.notes = value.toString();
                    break;
                }
                _rows[index.row()] = newRow;
                dataChanged(index, index);
        
                return true;
            }
            return false;
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Did you also re-implement the data method ? If so, how are you handling the EditRole there ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          K 1 Reply Last reply
          3
          • SGaistS SGaist

            Hi,

            Did you also re-implement the data method ? If so, how are you handling the EditRole there ?

            K Offline
            K Offline
            kitfox
            wrote on last edited by
            #5

            @SGaist I have now, and it's working. Thanks.

            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