CurrentItem and currentIndex for ListView



  • Hello guys,

    sorry for bothering you with this obvious for many question.
    But ... which item is considered to be current for the ListView element?

    I was trying to attach popup position to the coordinates of the pressed upon item in the list.
    But console keeps saying that currentIndex is 0 whichever event (onEntered, OnClicked etc) I'm trying to follow.

    Thank you in advance.



  • You can change currentIndex with methods: decrementCurrentIndex, incrementCurrentIndex. See Qt Assistant for details.



  • Initially by default the first (0 index) item will be current. The current item can be changed by the user with keystrokes (up, down arrow).

    Are you having a problem with what is reported as current? If so, can you provide a small example?



  • Maybe you should use index in onClicked since currentIndex is only changed when an item is selected in the list. If you have an MouseArea in your delegate index will be the index to the item in the list.



  • I believe that what confuses you is the fact, that an item you click doesn't change to current automatically. To do that you need to add a MouseArea to your delegate with this function:

    @MouseArea {
    anchors.fill: parent
    onClicked: {
    listView.currentIndex = index
    }
    }@

    "listView" is the id of the parent ListView component.



  • Thank you guys.
    I've got the idea.

    Thanks to your assistance I advanced a bit in my positioning task (I was trying to tie the popup position to the index of the clicked upon item)
    My snippet is quite basic (mouseArea is declared inside delegate):

    MouseArea {
    onClicked: { popup.y = index * delegateHeight } }

    But when we start dragging content of our Flickable (or ListView / GridView) this code is no longer correct. As index of the clicked upon item and index of the visible element are two different matters.
    Could you please help me again by hinting how to solve this problem?

    I tried this:
    popup.y = listview.contentY
    and this:
    popup.y = listview.visibleArea.yPosition * listview.height

    But evidently I do not understand those properties right.

    PS: index is a property ... of what QML element ?



  • [quote author="hyperborean72" date="1292239070"]My snippet is quite basic (mouseArea is declared inside delegate):

    MouseArea {
    onClicked: { popup.y = index * delegateHeight } }

    But when we start dragging content of our Flickable (or ListView / GridView) this code is no longer correct. As index of the clicked upon item and index of the visible element are two different matters.
    Could you please help me again by hinting how to solve this problem?

    I tried this:
    popup.y = listview.contentY
    and this:
    popup.y = listview.visibleArea.yPosition * listview.height

    But evidently I do not understand those properties right.[/quote]

    You could try using Item's mapping functions (mapToItem and mapFromItem) to translate between the delegate's position in its parent and the popup's intended position in its parent.

    [quote author="hyperborean72" date="1292239070"]PS: index is a property ... of what QML element ?
    [/quote]

    http://doc.qt.nokia.com/4.7/qdeclarativemodels.html gives more information on where the "index" property comes from.

    Regards,
    Michael



  • Thank you so much Michael.
    Mapping from one item to another worked.
    Though there is a minor deviation between expected and calculated this way positions



  • Thank you all.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.