Results 1 to 1 of 1

Thread: QML Image Zoom

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

    Default QML Image Zoom

    The Window of my program includes three parts (with blue,red,yellow background).
    I have an image in the red part.
    I use a PinchArea that contains a MouseArea in the photo to handle dragging and pinch zooming of the photo.
    The user can drag the image in the under yellow and red section, but I want User drag image just into red section.
    main.qml:
    Qt Code:
    1. RowLayout {
    2.  
    3. Rectangle {
    4. Layout.fillHeight: true
    5. Layout.fillWidth: true
    6. color: "red"
    7. ColumnLayout {
    8. anchors.fill: parent
    9. spacing: 0
    10. RowLayout {
    11. Layout.preferredHeight: 0.06*parent.height
    12. Layout.fillHeight: false
    13. Layout.fillWidth: true
    14. Rectangle {
    15. id:rectToolBar
    16. Layout.fillHeight: true
    17. Layout.fillWidth: true
    18. color: "#2196f3"
    19. ColumnLayout {
    20. anchors.fill: parent
    21. RowLayout {
    22. anchors.fill: parent
    23. Layout.fillWidth: true
    24. Layout.fillHeight: true
    25. ToolBar {
    26. anchors.fill: parent
    27. Layout.fillHeight: true
    28. Layout.alignment: Qt.RightToLeft
    29.  
    30. style: ToolBarStyle {
    31. background: Rectangle {
    32. color: "transparent"
    33. }
    34. }
    35.  
    36. RowLayout {
    37. anchors.fill: parent
    38.  
    39.  
    40. MyToolButton{
    41. id:tbCapture
    42. iconSource:"images/capture.png"
    43. MouseArea
    44. {
    45. id: maCapture
    46. anchors.fill: parent
    47. anchors.margins: -10
    48. hoverEnabled:true
    49. visible: camera.imageCapture.ready
    50.  
    51. onClicked: {
    52. camera.imageCapture.capture()
    53. }
    54.  
    55. cursorShape: Qt.PointingHandCursor
    56. }
    57. }
    58. }
    59. }
    60.  
    61.  
    62. }
    63. }
    64.  
    65.  
    66. }
    67. }
    68.  
    69. RowLayout {
    70. spacing: 0
    71. Layout.fillHeight: true
    72. Layout.fillWidth: true
    73.  
    74. Rectangle{
    75. Layout.fillHeight: true
    76. Layout.fillWidth: true
    77. color: "transparent"
    78.  
    79. Camera {
    80. id: camera
    81.  
    82. imageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlash
    83.  
    84. exposure {
    85. exposureCompensation: -1.0
    86. exposureMode: Camera.ExposurePortrait
    87. }
    88.  
    89. flash.mode: Camera.FlashRedEyeReduction
    90.  
    91. imageCapture {
    92. onImageCaptured: {
    93. photoPreview.source = preview // Show the preview in an Image
    94. }
    95.  
    96. }
    97.  
    98.  
    99. }
    100.  
    101. VideoOutput {
    102. id: viewfinder
    103. source: camera
    104. anchors.fill: parent
    105. focus : visible
    106. visible: !capture
    107. }
    108.  
    109. PhotoPreview {
    110. id : photoPreview
    111. anchors.fill : parent
    112. focus: visible
    113. visible: capture
    114. }
    115.  
    116. }
    117. }
    118. }
    119. }
    120.  
    121. Rectangle {
    122. id:inputRect
    123.  
    124. Layout.fillHeight: true
    125. Layout.preferredWidth: 0.3*parent.width
    126. color: "yellow"
    127.  
    128. }
    129. }
    To copy to clipboard, switch view to plain text mode 

    PhotoPreview.qml:
    Qt Code:
    1. Item {
    2. property alias source :image.source
    3. signal closed
    4. id:mywin
    5. property int highestZ: 0
    6. property real defaultSize: mywin.width
    7. property var currentFrame: undefined
    8. property real surfaceViewportRatio: 1.5
    9. ScrollView {
    10. anchors.fill: parent
    11. frameVisible: true
    12. highlightOnFocus: true
    13. style: ScrollViewStyle {
    14. transientScrollBars: true
    15. handle: Item {
    16. implicitWidth: 14
    17. implicitHeight: 26
    18. Rectangle {
    19. color: "#424246"
    20. anchors.fill: parent
    21. anchors.topMargin: 6
    22. anchors.leftMargin: 4
    23. anchors.rightMargin: 4
    24. anchors.bottomMargin: 6
    25. }
    26. }
    27. scrollBarBackground: Item {
    28. implicitWidth: 14
    29. implicitHeight: 26
    30. }
    31. }
    32.  
    33. Flickable {
    34. id: flick
    35. anchors.fill: parent
    36. contentWidth: parent.width
    37. contentHeight: parent.height
    38.  
    39. Rectangle {
    40. id: photoFrame
    41. width: parent.width
    42. height: parent.height
    43. color:"transparent"
    44. scale:defaultSize / parent.width
    45. Behavior on scale { NumberAnimation { duration: 200 } }
    46. Behavior on x { NumberAnimation { duration: 200 } }
    47. Behavior on y { NumberAnimation { duration: 200 } }
    48. smooth: true
    49. antialiasing: true
    50. Image {
    51. id:image
    52. anchors.fill: parent
    53. fillMode: Image.PreserveAspectFit
    54. smooth: true
    55. }
    56. PinchArea {
    57. anchors.fill: parent
    58. pinch.target: photoFrame
    59. pinch.minimumRotation: -360
    60. pinch.maximumRotation: 360
    61. pinch.minimumScale: 0.1
    62. pinch.maximumScale: 10
    63. pinch.dragAxis: Pinch.XAndYAxis
    64. property real zRestore: 0
    65. onSmartZoom: {
    66. if (pinch.scale > 0) {
    67. photoFrame.rotation = 0;
    68. photoFrame.scale = Math.min(mywin.width, mywin.height) / Math.max(image.sourceSize.width, image.sourceSize.height) * 0.85
    69. photoFrame.x = flick.contentX + (flick.width - photoFrame.width) / 2
    70. photoFrame.y = flick.contentY + (flick.height - photoFrame.height) / 2
    71. zRestore = photoFrame.z
    72. photoFrame.z = ++mywin.highestZ;
    73. } else {
    74. photoFrame.rotation = pinch.previousAngle
    75. photoFrame.scale = pinch.previousScale
    76. photoFrame.x = pinch.previousCenter.x - photoFrame.width / 2
    77. photoFrame.y = pinch.previousCenter.y - photoFrame.height / 2
    78. photoFrame.z = zRestore
    79. --mywin.highestZ
    80. }
    81. }
    82.  
    83. MouseArea {
    84. id: dragArea
    85. hoverEnabled: true
    86. anchors.fill: parent
    87. drag.target: photoFrame
    88. scrollGestureEnabled: false // 2-finger-flick gesture should pass through to the Flickable
    89. onPressed: {
    90. photoFrame.z = ++mywin.highestZ;
    91. }
    92.  
    93. onWheel: {
    94. if (wheel.modifiers & Qt.ControlModifier) {
    95. photoFrame.rotation += wheel.angleDelta.y / 120 * 5;
    96. if (Math.abs(photoFrame.rotation) < 4)
    97. photoFrame.rotation = 0;
    98. } else {
    99. photoFrame.rotation += wheel.angleDelta.x / 120;
    100. if (Math.abs(photoFrame.rotation) < 0.6)
    101. photoFrame.rotation = 0;
    102. var scaleBefore = photoFrame.scale;
    103. photoFrame.scale += photoFrame.scale * wheel.angleDelta.y / 120 / 10;
    104. }
    105. }
    106. }
    107. }
    108. }
    109. }
    110. }
    111. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by neda; 27th April 2016 at 08:50.

Similar Threads

  1. QSvgWidget in QMdiSubWindow: how to zoom the image
    By khaz in forum Qt Programming
    Replies: 0
    Last Post: 27th January 2016, 11:15
  2. Image Zoom in/out
    By Vivek1982 in forum Newbie
    Replies: 3
    Last Post: 22nd November 2013, 20:01
  3. How to zoom image correctely in QML?
    By chong_kimkeang in forum Newbie
    Replies: 1
    Last Post: 21st November 2012, 10:42
  4. Replies: 1
    Last Post: 9th October 2012, 12:32
  5. missing image with zoom (QGraphicsView)
    By avis_phoenix in forum Qt Programming
    Replies: 7
    Last Post: 18th March 2009, 13:09

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.