Results 1 to 6 of 6

Thread: How can QML understand mouse movement?

  1. #1
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    3
    Platforms
    Windows

    Default How can QML understand mouse movement?

    Hello guys,

    You know the fruit ninja. You can press anywhere and when you touch the fruit, animation plays. And if you know ActionScript 3.0, there is a hitTest to understand if it hit to another. In QML, how can we do this specific movement?

    OR:

    I want to draw a line: Touch screen, starts to draw a line to finish point which is point that you release touching.

    Any idea?

    Note: I'm using this following code but it works for only its own MouseArea and it's not what I need actually.

    Qt Code:
    1. Item {
    2. id: superrect
    3. anchors.horizontalCenter: myItem.horizontalCenter
    4. anchors.verticalCenter: myItem.verticalCenter
    5. width: myItem.width+60
    6. height: myItem.height+60
    7.  
    8. MouseArea {
    9. id: mouse
    10. anchors.fill: parent
    11. //onEntered: console.log("mouse entered the area")
    12. onExited: {
    13. myItem.opacity= 0;
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

  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: How can QML understand mouse movement?

    Have a MouseArea cover the whole touchable area and then test for a hit using a short javascript snippet that takes into account the current mouse position. Of course you need to know where your items are.
    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
    Jul 2012
    Posts
    26
    Thanks
    3
    Platforms
    Windows

    Default Re: How can QML understand mouse movement?

    Quote Originally Posted by wysota View Post
    Have a MouseArea cover the whole touchable area and then test for a hit using a short javascript snippet that takes into account the current mouse position. Of course you need to know where your items are.
    But the problem is that, when there are lots of items in the screen, it works for each item singly. And, it isn't my solution because of this.

  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: How can QML understand mouse movement?

    I don't understand what you mean, sorry... I'm sure the solution I offered to you is the proper way to go.


    Edit:

    Somewhat silly example but it works:

    javascript Code:
    1. import QtQuick 1.1
    2.  
    3. Rectangle {
    4.  
    5. property bool hit: false
    6.  
    7. function hitTest(posX, posY) {
    8. // dirty hit test
    9. if(posX >= obstacle.x && posY <= obstacle.x+obstacle.width
    10. && posY >= obstacle.y && posY <= obstacle.y+obstacle.height) {
    11. root.hit = true
    12. } else {
    13. root.hit = false
    14. }
    15. }
    16.  
    17. id: root
    18.  
    19. width: 360
    20. height: 360
    21.  
    22. MouseArea {
    23. anchors.fill: parent
    24. onPositionChanged: hitTest(mouse.x, mouse.y)
    25. onReleased: root.hit = false
    26. }
    27.  
    28. Rectangle {
    29. id: obstacle
    30. x: 200
    31. y: 100
    32. width: 50
    33. height: width
    34. radius: width/2
    35. color: root.hit ? "red" : "blue"
    36.  
    37. Behavior on x {
    38. NumberAnimation { duration: 500 }
    39. }
    40. Behavior on y {
    41. NumberAnimation { duration: 500 }
    42.  
    43. }
    44.  
    45. }
    46.  
    47. Text {
    48. visible: hit
    49. text: "Collision detected"
    50. anchors.left: parent.left
    51. anchors.bottom: parent.bottom
    52. }
    53. Timer {
    54. triggeredOnStart: true
    55. interval: 500
    56. running: true
    57. onTriggered: {
    58. obstacle.x = Math.max(0, Math.min(obstacle.x - 10 + Math.round(Math.random() *20), root.width))
    59. obstacle.y = Math.max(0, Math.min(obstacle.y - 10 + Math.round(Math.random() *20), root.height))
    60.  
    61. }
    62. repeat: true
    63. }
    64. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 18th July 2012 at 20:38.
    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. #5
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    3
    Platforms
    Windows

    Default Re: How can QML understand mouse movement?

    thank you, I'll try

    edit:
    you are best! thank you it works
    Last edited by Yonetici; 19th July 2012 at 00:14.

  6. #6
    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: How can QML understand mouse movement?

    Just make a smarter hit test, checking the bounding rect might not be enough for a good user experience.
    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.


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

    Yonetici (20th July 2012)

Similar Threads

  1. about the mouse movement on the touchsceen LCD
    By huzl in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 9th March 2012, 14:45
  2. Contain mouse movement within drawing window
    By rdrnws in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2010, 10:52
  3. Forwrad & Backward mouse movement
    By sujan.dasmahapatra in forum Newbie
    Replies: 1
    Last Post: 28th October 2009, 07:54
  4. Mouse movement problem in QGraphicsView
    By zgulser in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2009, 08:01
  5. Game mouse movement
    By chaosgeorge in forum Qt Programming
    Replies: 1
    Last Post: 3rd December 2006, 00:41

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.