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. How to access listview current item from other item?
Forum Updated to NodeBB v4.3 + New Features

How to access listview current item from other item?

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

    can i have listview's current item model info ?
    when i try some thing like this
    @//:D
    import QtQuick 2.0

    Rectangle
    {
    id: root;
    height: 500;
    width: 500;
    Column
    {
    ListView
    {
    id: lv;
    height: 400;
    width: root.width;
    clip: true;
    orientation: ListView.Horizontal

            model: ListModel{
                id: model;
                ListElement{ name :"1"; }
                ListElement{ name :"2"; }
                ListElement{ name :"3";}
                ListElement{ name :"4";}
            }
            delegate: com;
        }
        Text
        {
            id:txt;
            height:100;
            width:root.width;
            text: lv.currentItem.name;
        }
    }
    Component
    {
        id: com;
        Rectangle
        {
            id: rec
            height: 400;
            width: root.width;
            Text
            {
                id: rectext;
                anchors.centerIn: parent;
                text: model.name;
            }
        }
    }
    

    }
    @
    i have this error: "Unable to assign [undefined] to QString" :D

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on last edited by
      #2

      Hi, well you could get the warning while the list view is still populating the items, so there might be a warning because list.currentItem will be undefined at first, but it may work after that? some more info please :)
      Also currentItem refers to the QML Item (delegate Item) and not the model data of that item, so you might have to use list.currentIndex, e.g. you can try
      @
      model.get(list.currentIndex).name
      @

      1 Reply Last reply
      0
      • D Offline
        D Offline
        destrat18
        wrote on last edited by
        #3

        no! it's not work after that :|
        your example work but i still have problem:
        it's not updated after i change listview :|

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xander84
          wrote on last edited by
          #4

          what do you mean after you change list view!?
          ListView.currentIndex is not updated by itself, you have to take care of that, that might be the problem I think. read the documentation for "currentIndex", so it depends how you want to change it, by button, click or mouse hover or whatever, I can't tell you how to do that without more details about your app :)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            destrat18
            wrote on last edited by
            #5

            in simple way
            (that i can explain -> i have bad english , i khnow i must improve it :) )
            i want to khnow user is seeing which part of my listview
            i want to have that part's name
            if i change code to this
            @//:D
            import QtQuick 2.0

            Rectangle
            {
            id: root;
            height: 500;
            width: 500;
            Column
            {
            ListView
            {
            id: lv;
            height: 400;
            width: root.width;
            clip: true;
            orientation: ListView.Horizontal
            snapMode: ListView.SnapOneItem;// added new :D

                    model: ListModel{
                        id: model;
                        ListElement{ name :"1"; }
                        ListElement{ name :"2"; }
                        ListElement{ name :"3";}
                        ListElement{ name :"4";}
                    }
                    delegate: com;
                }
                Text
                {
                    id:txt;
                    height:100;
                    width:root.width;
                    text: lv.currentItem;
                }
            }
            Component
            {
                id: com;
                Rectangle
                {
                    id: rec
                    height: 400;
                    width: root.width;
                    Text
                    {
                        id: rectext;
                        anchors.centerIn: parent;
                        text: model.name;
                    }
                }
            }
            

            }
            @
            i want to know user is looking which item ?
            tanx alot for your helping my friend
            (i try my best to explain my prob
            i must to stop being lazy and start learning english :| )

            1 Reply Last reply
            0
            • D Offline
              D Offline
              destrat18
              wrote on last edited by
              #6

              why
              when i want to add
              @ MouseArea {
              anchors.fill: parent
              onClicked: {
              listView.currentIndex = index
              }
              }@
              to my component
              i have this error?
              "Invalid component body specification"

              1 Reply Last reply
              0
              • X Offline
                X Offline
                Xander84
                wrote on last edited by
                #7

                hey, I modified your code a little look at this:
                @
                //:D
                import QtQuick 2.0

                Rectangle
                {
                id: root;
                height: 500;
                width: 500;
                Column
                {
                ListView
                {
                id: lv;
                height: 400;
                width: root.width;
                clip: true;
                orientation: ListView.Horizontal
                snapMode: ListView.SnapOneItem;// added new :D

                        model: ListModel{
                            id: model;
                            ListElement{ name :"1"; }
                            ListElement{ name :"2"; }
                            ListElement{ name :"3";}
                            ListElement{ name :"4";}
                        }
                        delegate: com;
                    }
                    Text
                    {
                        id:txt;
                        height:100;
                        width:root.width;
                        text: lv.currentItem.name; // now you can use currentItem.name to access the property of the delegate
                    }
                }
                Component
                {
                    id: com;
                    Rectangle
                    {
                        id: rec
                        property alias name: rectext.text // added this property alias to access the text of the current list item
                        height: 400;
                        width: root.width;
                        Text
                        {
                            id: rectext;
                            anchors.centerIn: parent;
                            text: model.name;
                        }
                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                lv.currentIndex = index // you used listView.currentIndex here?
                            }
                        }
                    }
                }
                

                }
                @
                I've added a property to access the text and your MouseArea to the delegate, that works fine and you can use it, see my comments in the code.
                Might not be perfect because it only changes the index if you click on the item, not on you drag/swipe it, but that is another problem for now I think.. maybe you find a solution to that.

                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