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. Different behavior between setData() and user editing for QAbstractTableModel

Different behavior between setData() and user editing for QAbstractTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 775 Views 2 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    I wrote a class derived from QAbstractTableModel and my setData() function is like this:

    struct item_t
    {
        int idx;
        quint64 val;
    };
    
    bool ModelLocalWhiteList::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if (!index.isValid()) return false;
        if (index.row() >= _items.count()) return false;
    
        item_t item = _items.at(index.row());
        switch (role)
        {
        case Qt::EditRole:
            switch (index.column())
            {
            case MODEL_IDX: item.idx = static_cast<quint16>(value.toUInt()); break;
            case MODEL_VAL: item.val = static_cast<quint64>(value.toLongLong()); break;
            default: break;
            }
            break;
        }
    
        (_items)[index.row()] = item;
        emit dataChanged(index, index);
        return true;
    }
    

    I need to have a different behavior when I programmatically call setData(), i.e. when I fill the model and when the user edits the cells.

    Is there a specific role for this?

    My final goal is the following. The item.val is defined as quint64 but when I set it from the code I pass a literal value to the function. Instead, when the user edits it he should enter the data as an hex. Hence I should change the conversion to:

    item.val = static_cast<quint64>(value.toString().toLongLong(nullptr, 16));
    
    raven-worxR VRoninV 2 Replies Last reply
    0
    • M Mark81

      I wrote a class derived from QAbstractTableModel and my setData() function is like this:

      struct item_t
      {
          int idx;
          quint64 val;
      };
      
      bool ModelLocalWhiteList::setData(const QModelIndex &index, const QVariant &value, int role)
      {
          if (!index.isValid()) return false;
          if (index.row() >= _items.count()) return false;
      
          item_t item = _items.at(index.row());
          switch (role)
          {
          case Qt::EditRole:
              switch (index.column())
              {
              case MODEL_IDX: item.idx = static_cast<quint16>(value.toUInt()); break;
              case MODEL_VAL: item.val = static_cast<quint64>(value.toLongLong()); break;
              default: break;
              }
              break;
          }
      
          (_items)[index.row()] = item;
          emit dataChanged(index, index);
          return true;
      }
      

      I need to have a different behavior when I programmatically call setData(), i.e. when I fill the model and when the user edits the cells.

      Is there a specific role for this?

      My final goal is the following. The item.val is defined as quint64 but when I set it from the code I pass a literal value to the function. Instead, when the user edits it he should enter the data as an hex. Hence I should change the conversion to:

      item.val = static_cast<quint64>(value.toString().toLongLong(nullptr, 16));
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Mark81
      you are free to define custom roles and use them as you like to distinguish your cases.

      Just make sure your custom role starts off from the value of Qt::UserRole:

      enum ItemRoles {
          MyItemRole = Qt::UserRole+1
      };
      
      ...
      
      model->setData( index, data, MyItemRole );
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      M 1 Reply Last reply
      1
      • raven-worxR raven-worx

        @Mark81
        you are free to define custom roles and use them as you like to distinguish your cases.

        Just make sure your custom role starts off from the value of Qt::UserRole:

        enum ItemRoles {
            MyItemRole = Qt::UserRole+1
        };
        
        ...
        
        model->setData( index, data, MyItemRole );
        
        M Offline
        M Offline
        Mark81
        wrote on last edited by
        #3

        @raven-worx Ok, so as far as I understand the built-in edit of the cell will use EditRole. Simple enough to change my code, then. Thanks!

        1 Reply Last reply
        0
        • M Mark81

          I wrote a class derived from QAbstractTableModel and my setData() function is like this:

          struct item_t
          {
              int idx;
              quint64 val;
          };
          
          bool ModelLocalWhiteList::setData(const QModelIndex &index, const QVariant &value, int role)
          {
              if (!index.isValid()) return false;
              if (index.row() >= _items.count()) return false;
          
              item_t item = _items.at(index.row());
              switch (role)
              {
              case Qt::EditRole:
                  switch (index.column())
                  {
                  case MODEL_IDX: item.idx = static_cast<quint16>(value.toUInt()); break;
                  case MODEL_VAL: item.val = static_cast<quint64>(value.toLongLong()); break;
                  default: break;
                  }
                  break;
              }
          
              (_items)[index.row()] = item;
              emit dataChanged(index, index);
              return true;
          }
          

          I need to have a different behavior when I programmatically call setData(), i.e. when I fill the model and when the user edits the cells.

          Is there a specific role for this?

          My final goal is the following. The item.val is defined as quint64 but when I set it from the code I pass a literal value to the function. Instead, when the user edits it he should enter the data as an hex. Hence I should change the conversion to:

          item.val = static_cast<quint64>(value.toString().toLongLong(nullptr, 16));
          
          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @Mark81 said in Different behavior between setData() and user editing for QAbstractTableModel:

          when the user edits [...] I should change the conversion to:

          This kind of stuff is the job of the delegate. subclass QStyledItemDelegate, reimplement setModelData and do the conversion there.
          The model should not care about who/what is calling setData

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

          • Login

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