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. Accesing a qml item from a ListModel

Accesing a qml item from a ListModel

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 771 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.
  • M Offline
    M Offline
    mititelud
    wrote on last edited by
    #1

    hello,

    i have the following qml code:

    @
    Rectangle
    {
    id: bgRect
    width: 800
    height: 600
    objectName: "asd"
    Component
    {
    id: myDelegate

        Item
        {
            id:itm
            objectName: "item"+index;
            width: 40*(index+1)
            height: 40
            x:60-((index+1)*20)
    
            Rectangle
            {
    
                anchors.centerIn: parent
                anchors.fill: parent
                anchors.margins: 1
                radius: 5
                color: "lightsteelblue"
    
                Text
                {
                    text: "Disc "+model.ring
                    color: "black"
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.verticalCenter:  parent.verticalCenter
                }
    
                MouseArea
                {
                    anchors.fill: parent
                    onClicked:
                        console.log("objectName "+itm.objectName)
                }
    
                Component.onCompleted:
                    console.log(itm.objectName)
            }
        }
    }
    
    ListView
    {
        id: myListView
        anchors.fill: parent
        delegate: myDelegate
        model: myModel
        orientation: ListView.Vertical
        boundsBehavior: Flickable.StopAtBounds
    }
    

    }
    @

    and i am trying to acces the " objectName: "item"+index; " --line 16-- element with the following cpp code:

    @
    QObject object = (QObject)view->rootObject();
    QObject myObject = object->findChild<QObject>("item0");
    if(myObject)
    {
    //myObject->setProperty("x","50");
    qDebug()<<"myObject was created";
    myObject->setProperty("x","150");
    myObject->setProperty("color","green");
    }
    @

    and no luck so far, although, i can acces the "asd" rectangle from cpp. Please help.

    Thank you

    :)

    1 Reply Last reply
    0
    • O Offline
      O Offline
      onek24
      wrote on last edited by
      #2

      Hello,

      i would go ahead and add an objectName to your Model and access its method get(int index):
      "object ListModel:.get()":http://qt-project.org/doc/qt-5.0/qtqml/qml-qtquick2-listmodel.html#get-method

      Otherwise you could try to access your Models child instead.

      1 Reply Last reply
      0

      • Login

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