setData in ItemDelegate

  • 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));
            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?

  • Moderators

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

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

  • Moderators

    what do you think? :)

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

  • Im an Idiot... it works now. Thx

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.