Nominate our 2022 Qt Champions!

writing to an existing item within a C++ model to update QML listview

  • Hi

    I am using a similar model as described here. I need to update the value of an existing item . So i guess i need to setdata() and then emit datachanged() signal (please do correct me if I am wrong!!). Can someone guide me how to write the setdata() function.

    This is what I have written:

    I need to change only the size values so I have added a setdata() in the Animal class also.

    Function I have added to Animal class:*

    void Animal::setData(const QString size, int role){
           if(role == AnimalModel::SizeRole)
    void Animal::  setsize(const QString size){
        m_size = size;
    void AnimalModel::setdata(const QModelIndex &index,const QString &type,const QString &size){
    if (index.row() < 0 || index.row() >= m_animals.count())

    I get the error in this line :

    error: passing 'const Animal' as 'this' argument of 'void Animal::setData(QString, int)' discards qualifiers [-fpermissive],role);

Log in to reply