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

Access QAbstractItemModel in QML



  • Hello,

    I implemented a QAbstractItemModel just like in this exemple : https://wiki.qt.io/How_to_Use_a_Custom_Class_in_C%2B%2B_Model_and_QML_View

    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

    //main.cpp
     VarList vars;
      engine.rootContext()->setContextProperty("mod",&vars);
    
    //qml
         function goToVarId(_vind){  // how to implement this ?
             // find  the varId with value === _vind 
             // position view at that index 
    }
    
        ListView{
               model : mod.model
            delegate:Rectangle{
                height: 16*13
                width: parent.width
                Text{
                    height:13
                    text : "variable id : " + edit.varId + "<br>" +
                           "variable name : " + edit.varName + "<br>" +
                           "alias : " + edit.varAlias + "<br>" +
                           "slave id " + edit.slaveId                    
                }
            }
     }
    

    Thx


  • 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>();
            qDebug()<<v.varAlias();
            return v.varAlias();
        }
    

Log in to reply