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. ListView with ObjectModel and custom objects get index of selected object
Forum Updated to NodeBB v4.3 + New Features

ListView with ObjectModel and custom objects get index of selected object

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 861 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.
  • A Offline
    A Offline
    Aapje06
    wrote on last edited by
    #1

    Hello!

    Is it possible to get the index of the selected item in an ObjectModel that is displayed with an ListView?

    For example:

    // A.qml
    
    Item {
    id: itemA
    property string name;
    Text {
    text: "A:" + name
    }
    }
    
    // B.qml
    
    Item {
    id: itemB
    property string name;
    Text {
    text: "B:" + name
    }
    }
    
    
    //List.qml
    
    ListView {
    property var objects;
    id: listViewExample
    model: objects
    }
    
    
    // main.qml
    
    List {
    objects: ObjectModel {
    A { name: "test"}
    B { name: "test"}
    }
    }
    
    

    The reason is that i want to create multiple objects that can be displayed on one list, but each of them have a slightly different behaviour when clicked etc. This is a bit oversimplyfied here in this example.

    However, the question remains. How can I get the index of the object in the components A and B in the custom implementation in files A.qml and B.qml. Note: the objects A and B should only be used in combo with the custom listview in List.qml (that is the goal at least)

    Or from another perspective, how do I implement this cleanly to have a set of custom components in a list that notifies the above Listview when the custom component is clicked? The listview is displayed on a touchscreen that will scroll trough the list of items. And I want it to scroll to the selected item when selected.

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      I don't know why it doesn't say in the ListView docs, but you can use the "index" variable inside a delegate. I would assume you could use that in your A or B object inside an ObjectModel. I know you can use "index" inside a regular delegate.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        felsi
        wrote on last edited by
        #3

        I stumpled across this thread.
        The answer is quite simple:

        Item {
           id: itemA
           property string name;
        
           Text {
               text: "A: name:" + itemA.name + " index:" + itemA.ObjectModel.index;
           }
        }
        
        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