Important: Please read the Qt Code of Conduct -

[SOLVED] How to get QModelIndex needed for QAbstractTableModel::setData?

  • I have a QMap with values << ¨Register 1¨ << ¨Register 2¨ initialized as follow:

    QMap<QString, int> registermap;

    for (int i = 0; i < 10; i++)
    registermap.insert(QString("Register %1").arg(i), i);

    In my application, I have an event that signals me to update a particular row (keyed by ¨Register n¨) in the tableview. I am trying to get the QModelIndex needed as an input to QAbstractTableModel::setData

    QModelIndex currentIndex = registerModel->index(registermap[e->Desc], 0, QModelIndex());
    registerModel->setData(currentIndex, e->Value, Qt::EditRole);

    However, the above implementation always return the value of ¨1¨ even if the e->Desc is ¨Register 9¨... Can someone shine some light on me?

  • Hard to guess what's going on, we do not know what's your registerModel is based on.

  • disregard my post... I was handling the signal in the wrong object. What I needed to do was handle the signal in my inherited QAbstractTableModel object and do the following....

    public slots:
    void RegisterValueChanged(QObject* obj, RegData* e)
    registerMap[e->Desc] = e->Value;

    [EDIT: code formatting, Volker]

Log in to reply