Results 1 to 11 of 11

Thread: Adding mouse event tracking to a QLabel contained in a main window

  1. #1
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Adding mouse event tracking to a QLabel contained in a main window

    I have created a main window with Qt Creator containing a QLabel named tracking_frame ...

    How would I add mouse tracking to this QLabel only? - for example, outputting the coordinates (with respect to the QLabel) of a mouse click within the label.

    I realise that I need to overwrite the protected member function: void mousePressEvent(QMouseEvent *event) inside tracking_frame and then create a function like so:

    Qt Code:
    1. void ??? ::mousePressEvent(QMouseEvent *event)
    2. {
    3. fprintf(stderr,"(x,y)=(%d,%d)\n",event->pos().x(),event->pos().y());
    4. } // mousePressEvent()
    To copy to clipboard, switch view to plain text mode 

    I am just unsure of where to place the
    Qt Code:
    1. protected:
    2. void mousePressEvent(QMouseEvent *event);
    To copy to clipboard, switch view to plain text mode 
    (as the QLabel class definition is inside "ui_MainWindow.h" as it was all created in Qt Creator):

    If I place it in the MainWin class definition in MainWindow.h the mouse tracking works but of course for anywhere in the main window...

    What do I have to do?

    I hope someone can help me out with this!
    Hopefully it is a fairly straightforward procedure

    Cheers!
    Last edited by dmginc; 17th January 2010 at 10:00.

  2. The following user says thank you to dmginc for this useful post:

    ready (25th March 2011)

  3. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adding mouse event tracking to a QLabel contained in a main window

    If you want to catch mouse events from some widget you have to override mousePressEvent() in this widget:
    Qt Code:
    1. class MyLabel : public QLabel
    2. {
    3. // ...
    4. void mousePressEvent(QMouseEvent *event);
    5. };
    6.  
    7. void MyLabel::mousePressEvent(QMouseEvent *event)
    8. {
    9. // here you get event which happend on this label
    10. }
    To copy to clipboard, switch view to plain text mode 
    but if you don't want to subclass label you can use [qtclass]QObject::eventFilter()[qtclass] to filter events from your label. You can find examples and good explanation in QtAssistant.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  4. The following user says thank you to faldzip for this useful post:

    ready (25th March 2011)

  5. #3
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding mouse event tracking to a QLabel contained in a main window

    I see!

    Would I put this new class declaration for my label within the MainWin class declaration in "MainWindow.h" to subclass it?

    That's the part I'm unsure of...

    Thanks again!

  6. #4
    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: Adding mouse event tracking to a QLabel contained in a main window

    You can either add the tracking functionality to label, and use those label in your main window,

    Second option is - use event filters in main window, and check if the event is for the intended widget
    say if(mouseEvent->widget == tracking_frame ) and output the cordinates in the main window itself.

    Using filter in mainwindow will be better because in case you need to track mouse events for some other widgets which are not labels, you wont have to subclass them.

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

    ready (25th March 2011)

  8. #5
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding mouse event tracking to a QLabel contained in a main window

    Thanks for the reply!

    I think I will go with the sub-classing option because with event filtering, if I click on the QLabel, the coordinates will be relative to the entire main window, right? (not to the size of the QLabel)

    My real question is how do I subclass when the QLabel declaration is inside the header created by Qt Designer?

    In the UI file created with Designer, the QLabel object I want to modify is a public member of Ui_MainWindow:

    Qt Code:
    1. class Ui_MainWindow
    2. {
    3. public:
    4. //
    5. QLabel *tracking_frame;
    6. //
    7. //
    8. }
    To copy to clipboard, switch view to plain text mode 

    So my question is, how do I overwrite the mousePressEvent protected member?
    As in, what code do I insert into MainWindow.h? (or do I have to create a new header and source file to do this?)

    I tried just adding this above the MainWin class declaration in MainWindow.h:
    Qt Code:
    1. class tracking_frame : public QLabel, private Ui::MainWindow::tracking_frame
    2. {
    3. protected:
    4. void mousePressEvent(QMouseEvent *event);
    5. };
    To copy to clipboard, switch view to plain text mode 

    But this doesn't work...

    Thanks so much!

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

    ready (25th March 2011)

  10. #6
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Adding mouse event tracking to a QLabel contained in a main window

    or you can use installEventFilter(...)

    using which you can catch Qlabel mouse events in your mainwindow class

    something like this,

    Qt Code:
    1. label.installEventFilter(mainwindow);
    To copy to clipboard, switch view to plain text mode 

  11. #7
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding mouse event tracking to a QLabel contained in a main window

    Thanks to all the replies thus far!

    I've successfully got the mouse tracking working in only the QLabel widget in the main window - I created a subclass of QLabel and just overwrote the protected member function mousePressEvent()

    The only weird thing is:

    A small portion (rectangular-shaped) OUTSIDE the QLabel at the top left corner ALSO responds to mouse events whereas EVERYWHERE else outside the QLabel (correctly) does not... This area is clearly not a part of the QLabel...

    I can't work out why either - anyone have any ideas?

    Much appreciated!

  12. The following user says thank you to dmginc for this useful post:

    ready (25th March 2011)

  13. #8
    Join Date
    Mar 2011
    Posts
    36
    Thanks
    30
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Adding mouse event tracking to a QLabel contained in a main window

    dmginc
    please post your detail code as soon as possible ...
    I am also having same problem as your

  14. #9
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Adding mouse event tracking to a QLabel contained in a main window

    Quote Originally Posted by ready View Post
    please post your detail code as soon as possible ...
    I am also having same problem as your
    whats wrong with the solutions you were given in the thread you created by yourself?

    edit: ok, I see the new posts now... strange. I looked into the thread before positing here.

  15. #10
    Join Date
    Mar 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding mouse event tracking to a QLabel contained in a main window

    i had the same probelm...i went for subclassing....thanks

  16. #11
    Join Date
    Jul 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding mouse event tracking to a QLabel contained in a main window

    Hello All!!

    I am building a camera app using QT and openCV..... I need to create a mouse event on a QLabel inside a main window.... Sub Classing is not feasible for me.. so I went with using installEventFilter() ..... I checked and the mouse event is working fine but the image in the label is not visible.... can you tell me how to fix this problem??

    Thanks!!

Similar Threads

  1. mouse tracking on image
    By vermarajeev in forum Qt Programming
    Replies: 14
    Last Post: 12th May 2010, 14:06
  2. Mouse tracking outside the application interface
    By sophister in forum Qt Programming
    Replies: 7
    Last Post: 2nd May 2009, 07:44
  3. mouse tracking in QGraphicsItem
    By christina123y in forum Qt Programming
    Replies: 10
    Last Post: 9th March 2009, 09:23
  4. Adding scroll bars to a main window
    By dougab in forum Newbie
    Replies: 9
    Last Post: 25th December 2007, 21:27
  5. [QT3+XP] transparency and mouse tracking
    By incapacitant in forum Newbie
    Replies: 9
    Last Post: 17th February 2006, 19:49

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.