Results 1 to 1 of 1

Thread: Position of mouse on image

  1. #1
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Position of mouse on image

    Hi,
    I have two images:
    1- normal image
    2- same image with the ability to zoom

    I have 4 draggable shapes (A,B,C,D) on second image (image with the ability to zoom).

    I want to display position of mouse on normal image (with draw a shape (canvas3)) when the position of A/B/C/D shapes changed on the zoomed image.
    I used the following code but it does not show correct position.

    Qt Code:
    1. property int xwheel: 0
    2. property int ywheel: 0
    3. property alias source :image.source
    4. id:mywin
    5. property int highestZ: 0
    6. property real defaultSize: mywin.width
    7. property real surfaceViewportRatio: 1.5
    8.  
    9. ScrollView {
    10. anchors.fill: parent
    11. flickableItem.interactive: true
    12. frameVisible: true
    13. highlightOnFocus: true
    14. style: ScrollViewStyle {
    15. transientScrollBars: true
    16. .........
    17. }
    18.  
    19. Flickable {
    20. id: flick
    21. anchors.fill: parent
    22. contentWidth: parent.width
    23. contentHeight: parent.height
    24.  
    25. Rectangle {
    26. id: photoFrame
    27. width: parent.width
    28. height: parent.height
    29. scale:defaultSize / parent.width
    30. Behavior on scale { NumberAnimation { duration: 200 } }
    31. Behavior on x { NumberAnimation { duration: 200 } }
    32. Behavior on y { NumberAnimation { duration: 200 } }
    33. smooth: true
    34. antialiasing: true
    35.  
    36. Image {
    37. id:image
    38. anchors.fill: parent
    39. fillMode: Image.PreserveAspectFit
    40. smooth: true
    41. }
    42. PinchArea {
    43. anchors.fill: parent
    44. pinch.target: photoFrame
    45. pinch.minimumRotation: -360
    46. pinch.maximumRotation: 360
    47. pinch.minimumScale: 0.1
    48. pinch.maximumScale: 10
    49. pinch.dragAxis: Pinch.XAndYAxis
    50. property real zRestore: 0
    51. onSmartZoom: {
    52. if (pinch.scale > 0) {
    53. photoFrame.rotation = 0;
    54. photoFrame.scale = Math.min(mywin.width, mywin.height) / Math.max(image.sourceSize.width, image.sourceSize.height) * 0.85
    55. photoFrame.x = flick.contentX + (flick.width - photoFrame.width) / 2
    56. photoFrame.y = flick.contentY + (flick.height - photoFrame.height) / 2
    57. zRestore = photoFrame.z
    58. photoFrame.z = ++mywin.highestZ;
    59. } else {
    60. photoFrame.rotation = pinch.previousAngle
    61. photoFrame.scale = pinch.previousScale
    62. photoFrame.x = pinch.previousCenter.x - photoFrame.width / 2
    63. photoFrame.y = pinch.previousCenter.y - photoFrame.height / 2
    64. photoFrame.z = zRestore
    65. --mywin.highestZ
    66. }
    67. }
    68.  
    69. MouseArea {
    70. id: dragArea
    71. hoverEnabled: true
    72. anchors.fill: parent
    73. drag.target: photoFrame
    74. scrollGestureEnabled: false
    75. onPressed: {
    76. photoFrame.z = ++mywin.highestZ;
    77. }
    78.  
    79. onWheel: {
    80.  
    81. if (wheel.modifiers & Qt.ControlModifier) {
    82. photoFrame.rotation += wheel.angleDelta.y / 120 * 5;
    83. if (Math.abs(photoFrame.rotation) < 4)
    84. photoFrame.rotation = 0;
    85. } else {
    86. photoFrame.rotation += wheel.angleDelta.x / 120;
    87. if (Math.abs(photoFrame.rotation) < 0.6)
    88. photoFrame.rotation = 0;
    89. var scaleBefore = photoFrame.scale;
    90. photoFrame.scale += photoFrame.scale * wheel.angleDelta.y / 120 / 10;
    91. }
    92. }
    93. }
    94. }
    95.  
    96. Point {
    97. id: pointA
    98. x: image.width/4
    99. y: image.height/4
    100. onDragged: {
    101. xwheel=pointA.x;
    102. ywheel=pointA.y;
    103. canvas3.requestPaint()
    104. }
    105. }
    106. Point {
    107. id: pointB
    108. .....
    109. }
    110.  
    111. Point {
    112. id: pointD
    113. .....
    114. }
    115.  
    116. Point {
    117. id: pointC
    118. .....
    119. }
    120.  
    121. }
    122. }
    123. }
    124. Image {
    125. id:mainimage
    126. width: parent.width/4
    127. height: parent.height/4
    128. smooth: true
    129. source: image.source
    130.  
    131. Canvas {
    132. id: canvas3
    133. width: parent.width
    134. height: parent.height
    135. onPaint: {
    136. var context = getContext("2d");
    137.  
    138. // Make canvas all white
    139. ......
    140. context.beginPath();
    141. //Style
    142. .......
    143. context.moveTo(xwheel/4, ywheel/4);
    144. context.arc(xwheel/4, ywheel/4, 5, 0, 2*Math.PI, true)
    145. context.fill();
    146. context.stroke();
    147. }
    148. }
    149.  
    150. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by neda; 8th November 2016 at 04:46.

Similar Threads

  1. Getting Mouse position
    By williamjamir in forum Qwt
    Replies: 3
    Last Post: 8th October 2015, 20:05
  2. Mouse position
    By nomad.007 in forum Newbie
    Replies: 2
    Last Post: 8th September 2015, 16:11
  3. Lock Mouse Image Position
    By squeegedog in forum Qt Programming
    Replies: 1
    Last Post: 19th February 2014, 08:18
  4. view position of mouse position in GraphicsScene
    By Raghaw in forum Qt Programming
    Replies: 2
    Last Post: 23rd August 2012, 04:46
  5. How to get the mouse's position
    By sophister in forum Qt Programming
    Replies: 2
    Last Post: 30th April 2009, 06:07

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.