Results 1 to 10 of 10

Thread: dynamic circle instead of rectangle on mouse press and release event

  1. #1
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default dynamic circle instead of rectangle on mouse press and release event

    Hi Everyone,

    I had a post few days back http://http://www.qtcentre.org/threa...release-in-QML to draw a dynamic rectangle on mouse press and release event.

    I am using QtQuick1.1. Now instead of drawing rectangle dynamically on mouse press and release event, I want to draw a circle. any suggesstion will be highly appreciated.

    Thanksin advance
    jakr13

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamic circle instead of rectangle on mouse press and release event

    Two options:

    - a Rectangle with equal width/height and a radius half that value
    - a custom item

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    jakr13 (7th December 2015)

  4. #3
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamic circle instead of rectangle on mouse press and release event

    Hi anda_skoa,

    I went with the first method that you have mentioned and its working. Here is my code

    Qt Code:
    1. import QtQuick 1.1
    2.  
    3. Rectangle {
    4. id:referenceImageRect
    5. width: 300
    6. height: 300
    7.  
    8. property int pressX
    9. property int pressY
    10. property int releaseX
    11. property int releaseY
    12.  
    13. Image{
    14. id:referenceImage
    15. source:"file:///C://Users//Jakr13//Desktop//test.bmp"
    16. }
    17.  
    18. MouseArea {
    19. id:referenceImageRoiarea
    20. anchors.fill: parent
    21. acceptedButtons: Qt.LeftButton
    22.  
    23. onPressed: {
    24. pressX = mouse.x
    25. pressY = mouse.y
    26. console.log("Pressed Co-ordinates",pressX,pressY);
    27.  
    28. if (referenceRoi !== null) {
    29. referenceRoi.destroy();
    30. }
    31. referenceRoi = referenceRoiRect.createObject (referenceImageRoiarea, {"x" : pressX , "y" :pressY});
    32. }
    33.  
    34. onPositionChanged: {
    35. referenceRoi.width = (Math.abs (mouse.x - referenceRoi.x));
    36. referenceRoi.height = (Math.abs(mouse.y - referenceRoi.y));
    37. }
    38.  
    39. onReleased: {
    40. releaseX = mouse.x
    41. releaseY = mouse.y
    42. console.log("Released Co-ordinates",releaseX,releaseY);
    43.  
    44. }
    45. }
    46.  
    47. property Rectangle referenceRoi : null;
    48.  
    49. Component {
    50. id: referenceRoiRect;
    51.  
    52. Rectangle {
    53. id: rectroi
    54. color: "red";
    55. opacity: 0.7;
    56. x: pressX
    57. y: pressY
    58. width: releaseX - pressX;
    59. height: releaseY - pressY;
    60. radius: rectroi.width/2;
    61.  
    62. MouseArea {
    63. id:roiArea
    64. anchors.fill: parent
    65. acceptedButtons: Qt.RightButton
    66. hoverEnabled: true
    67.  
    68. onClicked: {
    69. console.log("Context menu");
    70. }
    71. }
    72. }
    73. }
    74.  
    75. }
    To copy to clipboard, switch view to plain text mode 

    I just want to know, is this the right way to do it? or is there any other way to do it more efficiently?

    Now i am planning to expand or contract or rotate the drawn shapes. Any suggestions on how to expand,contract or rotate will be highly appreciated?

    Thanks in advance!!!

    jakr13

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamic circle instead of rectangle on mouse press and release event

    Well, a circle requires that width and height are the same, which is currently not guaranteed with your code.
    I would say derive the radius as a minimum of your X and Y deltas and bind both width and height to 2 * radius.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    jakr13 (7th December 2015)

  7. #5
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamic circle instead of rectangle on mouse press and release event

    thanks anda_skoa, got it.

    any idea or example piece of code on how to rotate or expand or contract the drawn rectangle or circle? possibly as the extension of the above code?

    Thanks in advance

  8. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamic circle instead of rectangle on mouse press and release event

    Well, a rotated circle is just like the circle itself, unless you rotate around some other point than the center.
    In any case rotation is a standard property of any QtQuick item.

    I don't understand what you mean with expand or contract, the circle already does that when you modify its values during mouse movement, no?

    Cheers,
    _

  9. #7
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamic circle instead of rectangle on mouse press and release event

    Lets us say we have drawn a 50x25 rectangle over the image, but now we have changed our mind to expand 55x30 or contract 40x20 with the same rectangle drawn. How can I do that?

    thanks
    Jakr13

  10. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamic circle instead of rectangle on mouse press and release event

    Well, you simply change the width and height properties of the rectangle.

    Cheers,
    _

  11. #9
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamic circle instead of rectangle on mouse press and release event

    Hi anda_skoa,

    one more quick question, I will be selecting suppose say 5 circles and I want to destroy all the circles after pressing a button. Now I can able to delete only the last circle created. Lets take my previous program posted

    Qt Code:
    1. import QtQuick 1.1
    2.  
    3. Rectangle {
    4. id:referenceImageRect
    5. width: 300
    6. height: 300
    7.  
    8. property int pressX
    9. property int pressY
    10. property int releaseX
    11. property int releaseY
    12.  
    13. Image{
    14. id:referenceImage
    15. source:"file:///C://Users//Jakr13//Desktop//test.bmp"
    16. }
    17.  
    18. MouseArea {
    19. id:referenceImageRoiarea
    20. anchors.fill: parent
    21. acceptedButtons: Qt.LeftButton
    22.  
    23. onPressed: {
    24. pressX = mouse.x
    25. pressY = mouse.y
    26. console.log("Pressed Co-ordinates",pressX,pressY);
    27.  
    28. referenceRoi = referenceRoiRect.createObject (referenceImageRoiarea, {"x" : pressX , "y" :pressY});
    29. }
    30.  
    31. onPositionChanged: {
    32. referenceRoi.width = (Math.abs (mouse.x - referenceRoi.x));
    33. referenceRoi.height = (Math.abs(mouse.y - referenceRoi.y));
    34. }
    35.  
    36. onReleased: {
    37. releaseX = mouse.x
    38. releaseY = mouse.y
    39. console.log("Released Co-ordinates",releaseX,releaseY);
    40.  
    41. }
    42. }
    43.  
    44. property Rectangle referenceRoi : null;
    45.  
    46. Component {
    47. id: referenceRoiRect;
    48.  
    49. Rectangle {
    50. id: rectroi
    51. color: "red";
    52. opacity: 0.7;
    53. x: pressX
    54. y: pressY
    55. width: rectroi.radius *2;
    56. height: rectroi.radius *2;
    57. radius: Math.min((releaseX - pressx),(releaseY - pressY));
    58.  
    59. MouseArea {
    60. id:roiArea
    61. anchors.fill: parent
    62. acceptedButtons: Qt.RightButton
    63. hoverEnabled: true
    64.  
    65. onClicked: {
    66. console.log("Context menu");
    67. }
    68. }
    69. }
    70. }
    71.  
    72. Image {
    73. id:refresh
    74. source:"file:///C://Users//Jakr13//Desktop//refresh.bmp"
    75.  
    76. MouseArea {
    77. id:refreshArea
    78. anchors.fill: parent
    79. hoverEnabled: true
    80.  
    81. onClicked: {
    82. referenceRoi.destroy(); // destroying only the last object created
    83. }
    84. }
    85. }
    86.  
    87. }
    To copy to clipboard, switch view to plain text mode 

    How can I delete all the objects created by clicking on the refresh.bmp image?


    Thanks in advance!!!

  12. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamic circle instead of rectangle on mouse press and release event

    Store the newly created instance in an array, then iterate over the array to delete all objects in it.

    Cheers,
    _

Similar Threads

  1. Replies: 8
    Last Post: 26th November 2015, 09:10
  2. how to capture the F1 - key press/release event
    By sajis997 in forum Qt Quick
    Replies: 1
    Last Post: 6th January 2015, 22:25
  3. Drawing Rectangle with mouse press and release
    By Jakr1387 in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2014, 08:14
  4. Mouse Press/Release Events
    By Vivek1982 in forum Newbie
    Replies: 27
    Last Post: 22nd August 2014, 13:17
  5. Replies: 1
    Last Post: 12th November 2011, 16:40

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.