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. setData in ItemDelegate

setData in ItemDelegate

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.0k 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.
  • F Offline
    F Offline
    Fuel
    wrote on last edited by
    #1

    I have a QTableView and created a QStyledItemDelegate to make the Cells editable. Everything works fine, but i have one Problem. When i edit the Cell the new Value will not be set in the Model and after editing it shows me the old Value. I think the Problem is the setData Method of the Model. Maybe i use the wrong Role, but i tried Qt::EditRole and Qt::DisplayRole. Here is my Code.

    void MedisTableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
        QLineEdit *editLine = static_cast<QLineEdit*>(editor);
    
        if (index.column() == 1 || index.column() == 8)
        {
            QString value = editLine->text();
            model->setData(index, QVariant(value), Qt::DisplayRole);
        }
        else if (index.column() == 4)
        {
            QDate value = QDate::fromString(editLine->text(), "d. MMMM yyyy");
            model->setData(index, value);
        }
        else if (index.column() == 2 || index.column() == 3)
        {
            int value = editLine->text().toInt();
            model->setData(index, QVariant::fromValue(value));
        }
        else
            QStyledItemDelegate::setModelData(editor, model, index);
    }
    

    Someone has any Idea how to find the Problem? I outputed the new value with qDebug() and its always right. So there is something with the setData() Method. Maybe because i use an own Model?

    raven-worxR 1 Reply Last reply
    0
    • F Fuel

      I have a QTableView and created a QStyledItemDelegate to make the Cells editable. Everything works fine, but i have one Problem. When i edit the Cell the new Value will not be set in the Model and after editing it shows me the old Value. I think the Problem is the setData Method of the Model. Maybe i use the wrong Role, but i tried Qt::EditRole and Qt::DisplayRole. Here is my Code.

      void MedisTableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
      {
          QLineEdit *editLine = static_cast<QLineEdit*>(editor);
      
          if (index.column() == 1 || index.column() == 8)
          {
              QString value = editLine->text();
              model->setData(index, QVariant(value), Qt::DisplayRole);
          }
          else if (index.column() == 4)
          {
              QDate value = QDate::fromString(editLine->text(), "d. MMMM yyyy");
              model->setData(index, value);
          }
          else if (index.column() == 2 || index.column() == 3)
          {
              int value = editLine->text().toInt();
              model->setData(index, QVariant::fromValue(value));
          }
          else
              QStyledItemDelegate::setModelData(editor, model, index);
      }
      

      Someone has any Idea how to find the Problem? I outputed the new value with qDebug() and its always right. So there is something with the setData() Method. Maybe because i use an own Model?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Fuel said in setData in ItemDelegate:

      I think the Problem is the setData Method of the Model.

      why not showing the code of it then?

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

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Fuel
        wrote on last edited by
        #3

        euhm i didnt reimplement that Method in my Model. Is that the Problem?

        raven-worxR 1 Reply Last reply
        0
        • F Fuel

          euhm i didnt reimplement that Method in my Model. Is that the Problem?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @Fuel
          what do you think? :)

          How should the model know how to set the data into your custom data structure.

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

          1 Reply Last reply
          3
          • F Offline
            F Offline
            Fuel
            wrote on last edited by
            #5

            Im an Idiot... it works now. Thx

            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