Results 1 to 4 of 4

Thread: Label specific mousePressEvent

  1. #1
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Label specific mousePressEvent

    I have a dialog with two labels, side by side. Each has an image associated with it.

    There is a RubberBand for each label. I want the rectangles associated with the rubber bands to be the same height, but the starting point to be independent in the two labels.

    I created the dialog in Designer, and defined a class that inherits from it.

    So, in this class, I can provide implementations of mousePressEvent, mouseMoveEvent, and mouseReleaseEvent for the dialog. This works, but I can't figure out how to provide a unique version for each of the two labels in the dialog.

    I can "get there" (I think), by figuring out where each label is in the widget and associate the event with the appropriate label. But, it would probably be simpler to have a separate implementation of the event handlers for each label in the dialog. (And I see me needing to do this as I get better at Qt).

    Can this be done?

    Thanks,

    Dave Thomas

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Label specific mousePressEvent

    How do you intend to obtain two separate origins for the rubber bands? You would normally create a QRubberBand on mouse down, resize it on mouse move, and process/delete it on mouse up. With this arrangement clicking in one label to set the origin will not permit clicking in the label other to set the other origin without destroying the first rubber band.

  3. #3
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Label specific mousePressEvent

    The click will be over just one of the labels. So, the origin is redefined for that rubber band only. Whenever you click on a label, you set the origin for that label and it's used for subsequent clicks, if the mouse is not over it.

    As the mouse moves, both rubber bands grow from their independent origins. So, the rubber bands will always have the same size, but different origins.

    So, I'm writing code to associate a mouse click with one of the two labels/rubber bands. The one associated with the mouse click uses it's pos() as the origin, while the other uses the saved origin. Both rubber band as the mouse moves, and the geometry of both rubber bands is saved at mouseRelease.

    This is for setting the AOI of two separate cameras. Each camera's AOI must have the same number of rows and columns, for reasons I won't go into here. But, the size of the AOI can be defined, and moved over the raw images, basically cropping them to a small size--the same size for both cameras.

    The origin for the Rubber band will change in only one of the labels--the one that had the mouse click.

    Probably many other ways to do the same thing--I'm open for suggestions. This is just the way I did it using mouseCallbacks and drawLines in OpenCV. It works, so I figured I do the same using Qt.

    The click will be over just one of the labels. So, the origin is redefined for that rubber band only. Whenever you click on a label, you set the origin for that label and it's used for subsequent clicks, if the mouse is not over it.

    As the mouse moves, both rubber bands grow from their independent origins. So, the rubber bands will always have the same size, but different origins.

    So, I'm writing code to associate a mouse click with one of the two labels/rubber bands. The one associated with the mouse click uses it's pos() as the origin, while the other uses the saved origin. Both rubber band as the mouse moves, and the geometry of both rubber bands is saved at mouseRelease.

    This is for setting the AOI of two separate cameras. Each camera's AOI must have the same number of rows and columns, for reasons I won't go into here. But, the size of the AOI can be defined, and moved over the raw images, basically cropping them to a small size--the same size for both cameras.

    The origin for the Rubber band will change in only one of the labels--the one that had the mouse click.

    Probably many other ways to do the same thing--I'm open for suggestions. This is just the way I did it using mouseCallbacks and drawLines in OpenCV. It works, so I figured I do the same using Qt.

    The click will be over just one of the labels. So, the origin is redefined for that rubber band only. Whenever you click on a label, you set the origin for that label and it's used for subsequent clicks, if the mouse is not over it.

    As the mouse moves, both rubber bands grow from their independent origins. So, the rubber bands will always have the same size, but different origins.

    So, I'm writing code to associate a mouse click with one of the two labels/rubber bands. The one associated with the mouse click uses it's pos() as the origin, while the other uses the saved origin. Both rubber band as the mouse moves, and the geometry of both rubber bands is saved at mouseRelease.

    This is for setting the AOI of two separate cameras. Each camera's AOI must have the same number of rows and columns, for reasons I won't go into here. But, the size of the AOI can be defined, and moved over the raw images, basically cropping them to a small size--the same size for both cameras.

    The origin for the Rubber band will change in only one of the labels--the one that had the mouse click.

    Probably many other ways to do the same thing--I'm open for suggestions. This is just the way I did it using mouseCallbacks and drawLines in OpenCV. It works, so I figured I do the same using Qt.


    Added after 1 12 minutes:


    I have it working, by associating the mouse event based on it's x position relative to the right-most label. Remember, the two labels are side by side horizontally.

    If the pos().x() is greater than the label on the right side's origin, define the associated RubberBand's origin with that pos(). Else, associate with the RubberBand for the left side.

    Note that the left label is named "right", and the right label is named "left". While confusing when reading this bit of code, it's intentional, since the camera images are of a "Left Lane" and "Right Lane". But, the view is such that the screen images will be reversed.

    So, the test is for event->pos().x() relative to the left->pos().x(), not relative right->pos().x() as you might expect.

    Here's the code.

    Qt Code:
    1. AOIWindow::AOIWindow(QWidget *parent) : QDialog(parent)
    2. {
    3.  
    4. rb_left = NULL;
    5. rb_right = NULL;
    6.  
    7. setupUi(this);
    8. left_origin = left->pos();
    9. right_origin = right->pos();
    10.  
    11. }
    12.  
    13. void AOIWindow::mousePressEvent(QMouseEvent *event)
    14. {
    15. click_origin = event->pos();
    16.  
    17. // Determine which label the click was over and change the origin for the associated rubber band (only)
    18. if (click_origin.x() > left->pos().x())
    19. {
    20. left_origin.setX(click_origin.x() - left->pos().x());
    21. left_origin.setY(click_origin.y());
    22. }
    23. else
    24. {
    25. right_origin.setX( click_origin.x() - right->pos().x());
    26. right_origin.setY(click_origin.y());
    27. }
    28.  
    29. if (!rb_left)
    30. rb_left = new QRubberBand(QRubberBand::Rectangle, left);
    31. if (!rb_right)
    32. rb_right = new QRubberBand(QRubberBand::Rectangle, right);
    33.  
    34. rb_left->setGeometry(QRect(left_origin, QSize()));
    35. rb_left->show();
    36. rb_right->setGeometry(QRect(right_origin, QSize()));
    37. rb_right->show();
    38. }
    39. void AOIWindow::mouseMoveEvent(QMouseEvent *event)
    40. {
    41. QPoint mousePosition = event->pos();
    42.  
    43. // Use the y directly for the associated RubberBand.
    44. // Use a rectangle with same size at saved origin for other one
    45. if (mousePosition.x() > left->pos().x())
    46. {
    47. rb_left->setGeometry(QRect(left_origin, event->pos()).normalized());
    48. rb_right->setGeometry(QRect(right_origin,rb_left->size()));
    49. }
    50. else
    51. {
    52. rb_right->setGeometry(QRect(right_origin, event->pos()).normalized());
    53. rb_left->setGeometry(QRect(left_origin, rb_right->size()));
    54. }
    55. }
    To copy to clipboard, switch view to plain text mode 

    The header file:

    Qt Code:
    1. class AOIWindow : public QDialog , public Ui::AOIWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. AOIWindow(QWidget *parent=0);
    7. QPoint click_origin;
    8. QPoint left_origin;
    9. QPoint right_origin;
    10. QRubberBand *rb_left;
    11. QRubberBand *rb_right;
    12.  
    13. void AOIWindow::mousePressEvent(QMouseEvent *event);
    14. void AOIWindow::mouseReleaseEvent(QMouseEvent *event);
    15. void AOIWindow::mouseMoveEvent(QMouseEvent *event);
    16.  
    17. private slots:
    18.  
    19.  
    20. };
    To copy to clipboard, switch view to plain text mode 

    So, while this works, it might not be as robust as getting mouse events from each label object instead of the widget that contains them. And, I'm really trying to learn Qt as I go along, so I still want to know if it's possible to get mouse events for objects inside the widget instead of just the widget's mouse events-- and without changing files that will get clobbered by Designer.

    Not a big deal if it's not possible--I'm just trying to follow "best practices" where appropriate. I'm a "three day old" newbie, so suggestions are welcome!

    Thanks,

    Dave Thomas


    Added after 19 minutes:


    Well, mostly working.

    I still need to add offsets to adjust for where the origins of the label are vertically with respect to the widget's origin and the fact that the labels' x origns aren't exactly at 0 and widget_width/2.

    Still, the code should illustrate what I'm trying to do and why it would be nice to get the mouse events associated with, and relative to, each label object instead of the containing widget.

    Dave Thomas
    Last edited by davethomaspilot; 13th June 2012 at 12:15.

  4. #4
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Label specific mousePressEvent

    Wow, Qt and QtDesigner Rock!

    I was thinking I'd have to scale images to fit a window size, rescale when window size changed, etc. But NO! The images scale with the window! And, once I figured out how to do spacers in the layout, everything looks nice regardless of window size. NICE!

    Glad I decided to start getting on board.

    Dave Thomas

Similar Threads

  1. Replies: 5
    Last Post: 14th February 2011, 14:06
  2. webview from UI and mousepressevent
    By maston in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2010, 12:58
  3. mousePressEvent problem
    By jhowland in forum Qt Programming
    Replies: 9
    Last Post: 18th April 2010, 23:53
  4. MousePressEvent for QTextEdit
    By anju123 in forum Qt Programming
    Replies: 9
    Last Post: 16th August 2007, 06:08
  5. mousePressEvent
    By mickey in forum Newbie
    Replies: 3
    Last Post: 21st March 2006, 15:36

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.