Results 1 to 9 of 9

Thread: Dynamic rectangle on mouse press and release in QML

  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

    Question Dynamic rectangle on mouse press and release in QML

    Hi Everyone,

    I am a newbie to QML and I wanted to select some rectangle ROI's in an Image. I am getting co-ordinates during mouse press and release event. Now, I need to start drawing a rectangle during mouse press event and end the rectangle during mouse release event. I have done this using Qt Widgets application, but I was thinking to implement the same in QML. Can someone guide me to implement this.

    Here's my code

    Qt Code:
    1. import QtQuick 1.1
    2.  
    3. Rectangle {
    4. id:imageRect
    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.  
    14. Image{
    15. id:imagetoshow
    16. source:"file:///C://Users//Jakr13//Desktop//testimage.bmp"
    17. }
    18.  
    19. MouseArea {
    20. id:roiRectArea
    21. anchors.fill: parent
    22.  
    23. onPressed: {
    24. pressX = mouse.x
    25. pressY = mouse.y
    26. console.log("Pressed Co-ordinates",pressX,pressY);
    27. }
    28.  
    29. onReleased: {
    30. releaseX = mouse.x
    31. releaseY = mouse.y
    32. console.log("Released Co-ordinates",releaseX,releaseY);
    33. }
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    Please feel free to point if there is anything wrong with my code or the way I am implementing is completely wrong.

    Thanks in 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 rectangle on mouse press and release in QML

    The geometry values of your rectangle are fixed and do not use the values you get from the mouse.

    Cheers,
    _

  3. #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 rectangle on mouse press and release in QML

    Hi anda_skoa,

    Thanks for your reply. The fixed geometry is for displaying the image, and I have to draw rectangles over the image based on mouse press and release events like we can say QRubberband in Qt widgets. Please check the image attached. The border with green has fixed geometry and I have to draw ROI's like red ones.


    download.jpg
    Last edited by jakr13; 25th November 2015 at 13:06.

  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: Dynamic rectangle on mouse press and release in QML

    What is this code supposed to do? You're not drawing any rectangles using those coordinates.
    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
    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 rectangle on mouse press and release in QML

    Hi Wysota,

    I apologize for not being clear in my question. I didn't implement to draw rectangle yet using those co-ordinates and I dont know how to do that. Suggestions on how to implement that or a sample code will be helpful for me to understand.

  6. #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 rectangle on mouse press and release in QML

    Create a rectangle element that is a sibling or child of the mouse area, use the value to determine the rectangle's geometry.

    Cheers,
    _

  7. #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 rectangle on mouse press and release in QML

    I created a rectangle and passed the co-ordinates to draw a rectangle. Now its like when release mouse, a rectangle will be drawn, But I need to implement like, when a press mouse, I should start drawing rectangle and when I release mouse I should end the rectangle. Any suggestions?

    Qt Code:
    1. import QtQuick 1.1
    2.  
    3. Rectangle {
    4. id:imageRect
    5. width: 1920
    6. height: 988
    7.  
    8. property int pressX
    9. property int pressY
    10. property int releaseX
    11. property int releaseY
    12. property int widthRect
    13. property int heightRect
    14.  
    15.  
    16. Image{
    17. id:imagetoshow
    18. source:"file:///C://Users//Jakr13//Desktop//test1.bmp"
    19. }
    20.  
    21. MouseArea {
    22. id:roiRectArea
    23. anchors.fill: parent
    24. acceptedButtons: Qt.LeftButton
    25.  
    26. onPressed: {
    27.  
    28. pressX = mouse.x
    29. pressY = mouse.y
    30. console.log("Pressed Co-ordinates",pressX,pressY);
    31. }
    32.  
    33. onReleased: {
    34. releaseX = mouse.x
    35. releaseY = mouse.y
    36. console.log("Released Co-ordinates",releaseX,releaseY);
    37. widthRect = releaseX - pressX
    38. heightRect = releaseY - pressY
    39. console.log("width, height:",widthRect,heightRect);
    40.  
    41. }
    42. }
    43.  
    44. Rectangle {
    45. id:rectRoi
    46. opacity: 0.4
    47. x: pressX
    48. y: pressY
    49. width: widthRect
    50. height: heightRect
    51. color: "#ffffff"
    52.  
    53. MouseArea {
    54. id:roiarea
    55. anchors.fill: parent
    56. acceptedButtons: Qt.RightButton
    57.  
    58. onClicked:{
    59.  
    60. console.log("Right Button Clicked");
    61. }
    62. }
    63. }
    64. }
    To copy to clipboard, switch view to plain text mode 

  8. #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 rectangle on mouse press and release in QML

    MouseArea onPositionChanged signal handler

    Cheers,
    _

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

    jakr13 (26th November 2015)

  10. #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 rectangle on mouse press and release in QML

    Thanks anda_skoa, I got it working, I had MouseArea onPositionChanged Signal and component of Rectangle within the rectangle.

Similar Threads

  1. how to capture the F1 - key press/release event
    By sajis997 in forum Qt Quick
    Replies: 1
    Last Post: 6th January 2015, 22:25
  2. Drawing Rectangle with mouse press and release
    By Jakr1387 in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2014, 08:14
  3. Mouse Press/Release Events
    By Vivek1982 in forum Newbie
    Replies: 27
    Last Post: 22nd August 2014, 13:17
  4. Replies: 6
    Last Post: 27th January 2011, 17:06
  5. How catch key press and key release for entire application
    By hubbobubbo in forum Qt Programming
    Replies: 4
    Last Post: 1st June 2010, 20:53

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.