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)
            setsize(size);
        else
            return;
    }
    
    
    void Animal::  setsize(const QString size){
    
                  if(QString::compare(size,m_size,Qt::CaseInsensitive))
                           return;
        m_size = size;
    }
    
    void AnimalModel::setdata(const QModelIndex &index,const QString &type,const QString &size){
    
    Q_UNUSED(type)
    if (index.row() < 0 || index.row() >= m_animals.count())
                   return;
    
    m_animals.at(index.row()).setData(size,role);
    
    }
    

    I get the error in this line :

    error: passing 'const Animal' as 'this' argument of 'void Animal::setData(QString, int)' discards qualifiers [-fpermissive]
    m_animals.at(index.row()).setData(value,role);
    ^




Log in to reply
 

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