Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Data() is not called after the table model changes in QAbstractTableModel even after using dataChanged()



  • Even after I changed my tableModel and emitted dataChanged() UI did not
    update.
    Tried begin and endResetModel(), didn't help.
    Further from my debugging I see that after the model is changed, data() is not getting invoked.
    I Have used QVector<QVector<QString>> for model and am using TableView as part of QML.



  • @kradhika981
    This is not a question, it's just a reference to some old post. If you have some question about that, you should state what it is, clearly.

    Reading through for you, if you cannot resolve the dataChanged() issue then answer https://forum.qt.io/topic/50720/solved-qml-problem-with-tableview-refresh-on-model-update/3 states that begin/endResetModel() works. Whether that is acceptable for your case no-one can know as you haven't said anything....



  • Hey, I am posting for the first time, so maybe some issue. Issue I am facing is as follows :

    1. Even after I changed my tableModel and emitted dataChanged() UI did not
      update.
    2. Tried begin and endResetModel(), didn't help.
    3. Further from my debugging I see that after the model is changed, data() is not getting invoked.

    I Have used QVector<QVector<QString>> for model and am using TableView as part of QML.



  • @kradhika981
    Since others report that (reset model at least) does work, I imagine you'll be asked to show a snippet of the relevant code you are using. We do not even know what it is you are changing in your model, and so whether dataChanged() is even the right signal....



  • QML :

    TableView {
    model: TableModel {}
    }
    

    main.cpp

    qmlRegisterType<TableModel>("TableModel", 0, 1, "TableModel");
    

    TableModel.h

    QVector<QVector<QString>> m_Model;
    

    TableModel.cpp

    // Here is where data is getting set 
    bool TableModel::setData(const QModelIndex &indexPos, const QVariant &value, int role)
    {
        if(data(indexPos, role) == value)
            return false;
    
        QString key = m_Model.at(indexPos.row()).at(indexPos.column()-1);
        qDebug() << "KEY:" << key;
        m_Model.replace(indexPos.row(),{key, value});
        qDebug() << "Model Data Changed!!!" << m_Model;
        QModelIndex start = index(1,0);
        emit dataChanged(start, indexPos, {role});
        return true;
    }
    
    void changeModel() {
           setData(index(1,1), "abc");
    }
    

    It is a table with two columns , a key value pair kind and the data change wil be triggered from cpp function changeModel() . Currently I was changing just the second row (1st row after header ) 2nd column value.

    So In the Output of qDebug() << "Model data Changed!!!" , I am seeing the new model, but UI is not getting updated.


Log in to reply