Results 1 to 5 of 5

Thread: mouse position on repeater items

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

    Default mouse position on repeater items

    Hi Qt Masters,

    Here i am again consulting my current issue.

    I am currently implementing a level indicator, which is working perfectly already using the onclicked method.
    However, I wanted to add a drag capability, wherein the level will increase or decrease when dragged to the right or left, respectively.

    Btw, i am using a repeater to populate the images, here's the code:

    Qt Code:
    1. Repeater
    2. {
    3. property int level: -1
    4. id:levelRepeater
    5. model: 15
    6.  
    7. Image {
    8. source: "/images/bar.png"
    9. id: levelImg
    10.  
    11. MouseArea {
    12. id: levelArea
    13. anchors.fill: parent
    14. onClicked: { level = index }
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    Now, I wanted to implement the drag capability. I initially thought of using the mouseX position since this is positioned horizontally.
    I noticed that there were 25 pixels in every image, it has x values from 0-24.

    Qt Code:
    1. onMouseXChanged: { if (mouseX % 25 == 0) { level++ } } //this is only for dragging to the right
    To copy to clipboard, switch view to plain text mode 

    Then I noticed that when dragging to the left, the mouseX value decreases from the initial value to 0 then to negative values once it reaches to another mouse area on the left of the current index.

    Maybe, you already encountered this kind if issue please advise, any help is greatly appreciated.

    TIA.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: mouse position on repeater items

    So what is the question? Are you asking whether we saw anything like that or rather how to implement the drag properly?
    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: mouse position on repeater items

    Quote Originally Posted by wysota View Post
    So what is the question? Are you asking whether we saw anything like that or rather how to implement the drag properly?
    Sorry, I forgot to include my questions.

    I need help on implementing drag properly in this scenario wherein images are populated through repeater.

    I modified the MouseArea code:

    Qt Code:
    1. MouseArea
    2. {
    3. id: levelArea
    4. anchors.fill: parent
    5. hoverEnabled: true
    6. property real pressX: 0
    7. readonly property alias lvlPressed: levelArea.pressed
    8.  
    9. onClicked: { level = index }
    10.  
    11. onMouseXChanged: {
    12. if (lvlPressed) {
    13. if (mouseX < pressX && mouseX % 25 == 0) {
    14. volume--
    15. } else {
    16. if (mouseX % 25 == 0) {
    17. volume++
    18. }
    19. }
    20. }
    21. }
    22.  
    23. onPressed: {
    24. level = index
    25. pressX = mouseX
    26. }
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 

    Please advise how to improve this code. Thanks.

    OT: Is there a way to override the clip property of the parent, in the child element?
    Last edited by joko; 2nd December 2014 at 12:20.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: mouse position on repeater items

    I think this approach is completely wrong and thus I don't see a way to improve this code. In my opinion you should have a single MouseArea element for the whole area of the repeater and calculate the level based on where the user clicks/drags.

    Something along the lines of:

    javascript Code:
    1. import QtQuick 2.0
    2.  
    3. Item {
    4. id: root
    5. width: 800
    6. height: 200
    7.  
    8. property int currentLevel: 0
    9.  
    10. Row {
    11. id: row
    12. spacing: 5
    13. anchors.fill:parent
    14. Repeater {
    15. model: 10
    16. Rectangle {
    17. width: 50
    18. height: 200
    19. color: currentLevel >= index ? "red" : "transparent"
    20. border { color: "black"; width: 1 }
    21. }
    22. }
    23. }
    24. MouseArea {
    25. anchors.fill: row
    26. function xToIndex(xPos) { return xPos/55 }
    27. onClicked: {
    28. root.currentLevel = xToIndex(mouse.x)
    29. }
    30. onPositionChanged: root.currentLevel = xToIndex(mouse.x)
    31. }
    32. }
    33. }
    34. }
    35. MouseArea {
    36. anchors.fill: row
    37. function xToIndex(xPos) { return xPos/55 }
    38. onClicked: {
    39. root.currentLevel = xToIndex(mouse.x)
    40. }
    41. onPositionChanged: root.currentLevel = xToIndex(mouse.x)
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 
    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.


  5. The following user says thank you to wysota for this useful post:

    joko (3rd December 2014)

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

    Default Re: mouse position on repeater items

    Quote Originally Posted by wysota View Post
    I think this approach is completely wrong and thus I don't see a way to improve this code. In my opinion you should have a single MouseArea element for the whole area of the repeater and calculate the level based on where the user clicks/drags.

    Something along the lines of:

    javascript Code:
    1. import QtQuick 2.0
    2.  
    3. Item {
    4. id: root
    5. width: 800
    6. height: 200
    7.  
    8. property int currentLevel: 0
    9.  
    10. Row {
    11. id: row
    12. spacing: 5
    13. anchors.fill:parent
    14. Repeater {
    15. model: 10
    16. Rectangle {
    17. width: 50
    18. height: 200
    19. color: currentLevel >= index ? "red" : "transparent"
    20. border { color: "black"; width: 1 }
    21. }
    22. }
    23. }
    24. MouseArea {
    25. anchors.fill: row
    26. function xToIndex(xPos) { return xPos/55 }
    27. onClicked: {
    28. root.currentLevel = xToIndex(mouse.x)
    29. }
    30. onPositionChanged: root.currentLevel = xToIndex(mouse.x)
    31. }
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 
    As expected, you're the beast!

    Thank you very much!

Similar Threads

  1. Replies: 3
    Last Post: 18th June 2013, 22:29
  2. view position of mouse position in GraphicsScene
    By Raghaw in forum Qt Programming
    Replies: 2
    Last Post: 23rd August 2012, 04:46
  3. Position of Items in QGraphicsScene/QGraphicsView
    By StefanK2 in forum Qt Programming
    Replies: 11
    Last Post: 7th July 2009, 14:04
  4. How to get the mouse's position
    By sophister in forum Qt Programming
    Replies: 2
    Last Post: 30th April 2009, 06:07
  5. How to get mouse's position?
    By coralbird in forum Newbie
    Replies: 4
    Last Post: 23rd July 2006, 03:52

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.