Important: Please read the Qt Code of Conduct -

Access QAbstractItemModel in QML

  • Hello,

    I implemented a QAbstractItemModel just like in this exemple :

    My probleme is that i can't find the way to access that model in a javascript function

    With a Qml ListModel it would be : modelName.get(index).roleName

     VarList vars;
         function goToVarId(_vind){  // how to implement this ?
             // find  the varId with value === _vind 
             // position view at that index 
               model : mod.model
                height: 16*13
                width: parent.width
                    text : "variable id : " + edit.varId + "<br>" +
                           "variable name : " + edit.varName + "<br>" +
                           "alias : " + edit.varAlias + "<br>" +
                           "slave id " + edit.slaveId                    


  • Moderators

    Add a Q_INVOKABLE method in C++ which will return the row for given _vind. Then use it in your QML function to set the current index to your desired position.

  • @sierdzio hi, thank you. I did as you suggested

        Q_INVOKABLE QString getAlias(int ind){
            QModelIndex nIndex = m_model->index(ind,0);
            QVariant l=m_model->data(nIndex);
            AbsKemVar v = l.value<AbsKemVar>();
            return v.varAlias();

Log in to reply