Results 1 to 10 of 10

Thread: Get QRect of a widget using mouseevent

  1. #1
    Join Date
    Apr 2008
    Posts
    35
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Get QRect of a widget using mouseevent

    Hi,

    I have a grid layout with two widgets displaying input and output image, i want to be able to draw a rectangle, like the one used in paint, only for output image with mouse button clicked and get the x,y,height,width (QRect) of the part selected when mouse button released.

    I read the document for mousereleaseevent and from that i can get the position of the mouse, but what i want is the rectangle selection, how do i draw this rectangle and return the Qrect value??

    I have no idea how to start with mouseevents or graphics items or such, please could be give me a example or a approach i can use to program this.

    Thanks

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get QRect of a widget using mouseevent

    If I understand your thinking, you want to draw a selection rectangle when the mouse is over your output image.

    First you have to get the position of the mouse using QMouseEvent:os () or QMouseEvent::globalPos () ( read the doc).

    Once you have the position of the mouse cursor, relative to the widget that received the even ( using those functions), you can easily retrieve the rect geometry and draw whatever you want (throught the paintEvent of the concerned widget).

  3. #3
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get QRect of a widget using mouseevent

    Have a look at QWidget::mousePressEvent(), QWidget::mouseMoveEvent() or QWidget::mouseReleaseEvent.
    Depending on when you want your custom drawing triggered.

  4. #4
    Join Date
    Apr 2008
    Posts
    35
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Get QRect of a widget using mouseevent

    Ok, I understand pos and globalPos from the doc..

    imageView->pos() //Gives me the widget position
    event->globalPos() // gives me the mouse position

    if mouse is within widget, then enable selection rectangle... on mouse release return get the mouse pos() and calculate Qrect.

    this is very straight forward. I still dont know how to enable selection rectangle.. I am using QLabel right now, but can switch to QGraphicsView if required..
    Last edited by arpspatel; 7th March 2010 at 17:08.

  5. #5
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get QRect of a widget using mouseevent

    QGraphicsView is a bit too heavy just to draw a rectangle.
    Create a flag (bool isOverImage) to indicate that the cursor is within the right widget (in QWidget::mousePressEvent(), QWidget::mouseMoveEvent() or QWidget::mouseReleaseEvent depending on the expected behaviour) and call update().
    In the paint method of that widget draw your selected rectangle, using this flag .
    Qt Code:
    1. if (isOverImage) drawRect(selectionrect.x(), selectionrect.y(), selectionrect.width(), selectionrect.height() );
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Apr 2008
    Posts
    35
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Get QRect of a widget using mouseevent

    Qt Code:
    1. void theGui::mousePressEvent(QMouseEvent *event){
    2. if (event->button() == Qt::LeftButton) {
    3. if((event->pos().x() < bx && event->pos().x() > tx) &&
    4. (event->pos().y() < by && event->pos().y() > ty)){
    5. startPoint = event->pos();
    6. drawing = true;
    7. }
    8. }
    9. }
    10.  
    11. void theGui::mouseReleaseEvent(QMouseEvent *event){
    12. if (event->button() == Qt::LeftButton && drawing) {
    13. if((event->pos().x() < bx && event->pos().x() > tx) &&
    14. (event->pos().y() < by && event->pos().y() > ty)){
    15. endPoint = event->pos();
    16. drawing = false;
    17. }
    18. }
    19. saveSelection(QRect(startPoint,endPoint));
    20. }
    21.  
    22. void theGui::mouseMoveEvent(QMouseEvent *event){
    23. if ((event->buttons() & Qt::LeftButton) && drawing){
    24. drawRect(startPoint,event->pos());
    25. }
    26. }
    27.  
    28. void theGui::paintEvent(QPaintEvent *event)
    29. {
    30. QPainter painter(this);
    31. QRect dirtyRect = event->rect();
    32. painter.drawImage(dirtyRect, image, dirtyRect);
    33. }
    34.  
    35. void theGui::drawRect(QPoint _s, QPoint, _e){
    36. QPainter painter(&image);
    37. painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
    38. Qt::RoundJoin));
    39. painter.drawRect(QRect(_s,_e));
    40. modified = true;
    41.  
    42. int rad = (myPenWidth / 2) + 2;
    43. update(QRect(_s,_e).normalized()
    44. .adjusted(-rad, -rad, +rad, +rad));
    45. }
    To copy to clipboard, switch view to plain text mode 

    Here is my code, how i wrote it..
    but i am getting a lot of squares and I dont want to paint a rectangle, i just want a selection rectangle which will disappear after selection is done..

    Thanks
    Attached Images Attached Images
    Last edited by arpspatel; 7th March 2010 at 20:48.

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

    seltra (3rd October 2010)

  8. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Get QRect of a widget using mouseevent

    Wont QRubberBandbe of some use to you

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

    arpspatel (8th March 2010)

  10. #8
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get QRect of a widget using mouseevent

    You got the idea,

    Call update() in void theGui::mouseMoveEvent(QMouseEvent *event), not in theGui::drawRect(QPoint _s, QPoint, _e) (rename it drawRectSelection(QPoint _s, QPoint, _e) to avoid confusion QPainter:rawRect(etc...)) .
    No need to calll drawRect(startPoint,event->pos()) in void theGui::mouseMoveEvent(QMouseEvent *event);
    Use your flag in theGui:aintEvent(QPaintEvent *event) and do the painting of the selection rectangle in this function only.


    Or you can use QRubberBand as suggested...

  11. The following user says thank you to toutarrive for this useful post:

    arpspatel (8th March 2010)

  12. #9
    Join Date
    Apr 2008
    Posts
    35
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Get QRect of a widget using mouseevent

    Qt Code:
    1. void theGui::mousePressEvent(QMouseEvent* event) {
    2. if (!rubberband)
    3. rubberband = new QRubberBand(QRubberBand::Rectangle, this);
    4. startPoint = event->pos();
    5. rubberband->setGeometry(QRect(startPoint,QSize()));
    6. rubberband->show();
    7. rubberBandActive = true;
    8. }
    9.  
    10.  
    11. void theGui::mouseMoveEvent(QMouseEvent* event) {
    12. if(rubberBandActive){
    13. rubberband->setGeometry(QRect(startPoint,event->pos()).normalized());
    14. }
    15. }
    16.  
    17. void theGui::mouseReleaseEvent(QMouseEvent* event) {
    18. if(rubberBandActive){
    19. endPoint = event->pos();
    20. rubberBandActive = false;
    21. rubberband->hide();
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    Thanks Guys... I tried the QRubberBand and it worked very well and on first try.. there is also an example in qt doc. I didnt even know there was a class like QRubberBand, so I have posted the code here just in case some one needs it.....
    Last edited by arpspatel; 8th March 2010 at 22:01.

  13. #10
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Get QRect of a widget using mouseevent

    I tried the QRubberBand and it worked very well and on first try.. there is also an example in qt doc. I didnt even know there was a class like QRubberBand
    Thats cuteness of Cute(Qt) ! You think, and its probable its there !

Similar Threads

  1. difference of QRect and QRectF
    By gbmtoday in forum Newbie
    Replies: 1
    Last Post: 14th January 2010, 22:52
  2. Pass mouseEvent to sibling widget?
    By nish in forum Qt Programming
    Replies: 5
    Last Post: 1st September 2009, 13:00
  3. Replies: 11
    Last Post: 17th January 2009, 09:06
  4. Move QCompleter on QRect x,y
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 10:33
  5. Replies: 1
    Last Post: 14th April 2007, 13:42

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.