Results 1 to 14 of 14

Thread: Handling mouse events

  1. #1
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Handling mouse events

    Hi,

    I have been trying to get a QLabel widget to handle mouse left click events. The label is in a layout, alongside other widgets. My strategy was to subclass QLabel and create a MyLabel widget, which contains the virtual methods for handling mouseEvents. However, when I run the application, the label does not handle any click events. On the header file of MyLabel I put:

    protected:
    virtual void mousePressEvent(QMouseEvent *event);


    On the MyLabel.cpp file I wrote the following definition for the mousePressEvent:

    void MyLabel::mousePressEvent(QMouseEvent *event)
    {
    if (event->button()==Qt::leftButton){

    //code for the mouse click event
    }
    }

    What is missing in this implementation? How could I make the label respond to mouse clicks and execute the code for the mouse click event?

    Thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Handling mouse events

    Typo Qt::LeftButton? Following works fine for me:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MyLabel : public QLabel
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. MyLabel(QWidget *parent = 0) : QLabel(parent)
    9. {}
    10.  
    11. protected:
    12. virtual void mousePressEvent(QMouseEvent *event)
    13. {
    14. if (event->button()==Qt::LeftButton)
    15. {
    16. qWarning() << Q_FUNC_INFO;
    17. }
    18. }
    19. };
    20.  
    21. int main(int argc, char *argv[])
    22. {
    23.  
    24. QApplication app(argc, argv);
    25.  
    26. MyLabel l;
    27. l.setText("foo bar");
    28. l.show();
    29.  
    30. return app.exec();
    31. }
    32.  
    33. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

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

    Maluko_Da_Tola (27th August 2010)

  4. #3
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Handling mouse events

    Thank you very much! I just not sure about #include "main.moc". What does it mean?

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Handling mouse events

    You don't need that normally when you put one class in one file, but since I put all in one file I must tell qmake explicit that it calls moc on that file. moc is needed to get all QObject features to work.

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

    Maluko_Da_Tola (26th August 2010)

  7. #5
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Handling mouse events

    Thank you! The only thing I don't understand is that when I try to define MyLabel in a separate header file, it wont compile! Do you know why is this happening?

    Cheers

  8. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Handling mouse events

    Can you post your exact code you use.

  9. #7
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Handling mouse events

    Sure. I created a myLabel.h header file that contains the following:

    #ifndef MYLABEL_H
    #define MYLABEL_H

    #include <QLabel>

    class MyLabel : public QLabel
    {
    Q_OBJECT
    public:

    protected:
    virtual void mousePressEvent(QMouseEvent *event)
    {
    if (event->button()==Qt::LeftButton)
    {
    qWarning() << Q_FUNC_INFO;
    }
    }
    };

    #endif // MYLABEL_H

    And in the main.cpp I wrote:

    #include <QtGui>
    #include "myLabel.h"


    int main(int argc, char *argv[])
    {

    QApplication app(argc, argv);


    MyLabel l;
    l.setText("foo bar");
    l.show();

    return app.exec();
    }


    Cheers

  10. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Handling mouse events

    ...and the error you get is???

    I guess because qWarning() is not defined. In your header include
    Qt Code:
    1. #include <QtGlobal>
    To copy to clipboard, switch view to plain text mode 
    and it should work.

  11. The following 2 users say thank you to Lykurg for this useful post:

    Maluko_Da_Tola (27th August 2010), sparticus_37 (8th September 2010)

  12. #9
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Handling mouse events

    It still does not work, even after the inclusion of #include <QtGlobal> in my header file. The error I get is:


    /home/maluko/Documents/Qt creator/MouseEvents3/main.cpp:0: Warning: No relevant classes found. No output generated.
    /home/maluko/Documents/Qt creator/MouseEvents3/myLabel.h:7: undefined reference to `vtable for MyLabel'
    /home/maluko/Documents/Qt creator/MouseEvents3/myLabel.h:7: undefined reference to `vtable for MyLabel'
    /home/maluko/Documents/Qt creator/MouseEvents3/myLabel.h:7: undefined reference to `vtable for MyLabel'
    /home/maluko/Documents/Qt creator/MouseEvents3/myLabel.h:7: undefined reference to `vtable for MyLabel'
    :-1: error: collect2: ld returned 1 exit status

  13. #10
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Handling mouse events

    Clean your project, run qmake and rebuild everything.
    That error means that a moc file didn't get created.

  14. The following user says thank you to tbscope for this useful post:

    Maluko_Da_Tola (27th August 2010)

  15. #11
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Handling mouse events

    I just did that but now I get the following errors:

    /home/maluko/Documents/Qt creator/MouseEvents3/moc_myLabel.cpp:10: In file included from moc_myLabel.cpp:10:
    /home/maluko/Documents/Qt creator/MouseEvents3/myLabel.h:14: error: invalid use of incomplete type ‘struct QMouseEvent’
    /usr/include/qt4/QtGui/qwidget.h:76: error: forward declaration of ‘struct QMouseEvent’
    /home/maluko/Documents/Qt creator/MouseEvents3/myLabel.h:16: error: invalid use of incomplete type ‘struct QDebug’
    /usr/include/qt4/QtCore/qglobal.h:1636: error: forward declaration of ‘struct QDebug’
    /usr/include/qt4/QtCore/qglobal.h:1640: warning: inline function ‘QDebug qWarning()’ used but never defined

  16. #12
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Handling mouse events

    These errors mean that some include files were not added to your mylabel.h file.

    Specifically, the include files for the classes QMouseEvent and QDebug.
    Add these include files to mylabel.h and the errors will go away.

  17. The following user says thank you to tbscope for this useful post:

    Maluko_Da_Tola (27th August 2010)

  18. #13
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Handling mouse events

    Thank you very much! It finally works!

  19. #14
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Handling mouse events

    Now that your query has been answered...why exactly are you using clickable labels versus other, more obviously user-interactive widgets?

Similar Threads

  1. Handling mouse events in Qt WebKit
    By yakin in forum Qt Programming
    Replies: 6
    Last Post: 21st April 2011, 05:44
  2. Handling mouse click events in QGraphicsItem
    By Luc4 in forum Qt Programming
    Replies: 7
    Last Post: 5th March 2010, 16:12
  3. mouse events handling with QGraphicsItem
    By trallallero in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2009, 14:15
  4. Handling Mouse Events of a parent widget
    By dvmorris in forum Qt Programming
    Replies: 2
    Last Post: 28th March 2007, 18:44
  5. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13

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.