Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Access QAbstractItemModel in QML
Forum Updated to NodeBB v4.3 + New Features

Access QAbstractItemModel in QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 764 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      ODБOïO 1 Reply Last reply
      2
      • sierdzioS sierdzio

        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.

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        @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();
            }
        
        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved