Results 1 to 4 of 4

Thread: get mouse coordinates in ListView

  1. #1
    Join Date
    Oct 2014
    Posts
    104
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default get mouse coordinates in ListView

    Hi Qt Masters,

    I'm having problem displaying a rectangle when pressing and holding on selected delegate item.

    Qt Code:
    1. Item {
    2. id: listItem
    3.  
    4. anchors {
    5. top: oparent.top
    6. left: parent.left
    7. right: parent.right
    8. bottom: parent.bottom
    9. }
    10.  
    11. ListView {
    12. id: list
    13.  
    14. anchors.fill: parent
    15.  
    16. currentIndex: -1
    17. clip: true
    18.  
    19. model: itemModel
    20. delegate: ItemDelegate {
    21. MouseArea {
    22. id: area
    23. anchors.fill: parent
    24. onPressAndHold: {
    25. menu.showed = true
    26. menu.x = mouseX
    27. menu.y = mouseY
    28. }
    29. }
    30. }
    31. boundsBehavior: Flickable.StopAtBounds
    32.  
    33. CustomMenu {
    34. id: menu
    35.  
    36. z: 100
    37.  
    38. parent: list
    39.  
    40. model: menuModel
    41. }
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

    In the code above, it returns only the mouse coordinates on selected item that is why the menu always showed on top part of the Item.
    When I tried putting mousearea on listItem, the ListView will not work.

    Please advice, thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: get mouse coordinates in ListView

    You need to put the mouse area on the list view and also make sure all mouse events are propagated from that mouse area to the view by ignoring incoming events in the mouse area element.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2014
    Posts
    104
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: get mouse coordinates in ListView

    Quote Originally Posted by wysota View Post
    You need to put the mouse area on the list view and also make sure all mouse events are propagated from that mouse area to the view by ignoring incoming events in the mouse area element.
    Thank you for your response.

    However, what I did was put the MouseArea on delegate item then send a signal from onPressAndHold with mouseX and mouseY parameters to the ListView.
    Then used mapFromItem on the listItem.

    Qt Code:
    1. Item {
    2. id: listItem
    3.  
    4. anchors {
    5. top: parent.top
    6. left: parent.left
    7. right: parent.right
    8. bottom: parent.bottom
    9. }
    10.  
    11. ListView {
    12. id: list
    13.  
    14. anchors.fill: parent
    15.  
    16. currentIndex: -1
    17. clip: true
    18.  
    19. model: itemModel
    20. delegate: ItemDelegate {
    21. onMenuPosition: {
    22. var pos = listItem.mapFromItem(list.currentItem, mouseX, mouseY)
    23. menu.x = pos.x
    24. menu.y = pos.y
    25. }
    26. }
    27. boundsBehavior: Flickable.StopAtBounds
    28.  
    29. CustomMenu {
    30. id: menu
    31.  
    32. z: 100
    33.  
    34. parent: list
    35.  
    36. model: menuModel
    37. }
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: get mouse coordinates in ListView

    That's much slower and definitely breaks encapsulation.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 5
    Last Post: 3rd March 2016, 15:42
  2. Replies: 2
    Last Post: 29th March 2013, 03:50
  3. tracking mouse coordinates
    By lightning2911 in forum Newbie
    Replies: 8
    Last Post: 11th December 2011, 23:51
  4. how to get mouse coordinates from Qgraphicsview
    By ready in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2011, 05:33
  5. getting mouse coordinates
    By eric in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2007, 19:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.