Results 1 to 3 of 3

Thread: Subclassing QLabel

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

    Default Subclassing QLabel

    Hi,

    I am trying to sublass QLabel to create a label capable of handling mouse click events. The following code works fine:

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


    What I don't understand is, when I try to define MyLabel class in a seperate header file, Qt does not compile anymore! Why is this happening?

    Thank you
    Last edited by wysota; 28th August 2010 at 11:11. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Subclassing QLabel

    What errors do you get?

    You don't need to "#include main.moc" if you have the class in a separate file.

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

    Maluko_Da_Tola (27th August 2010)

  4. #3
    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: Subclassing QLabel

    Really, you ought not to define functions in header files. You can, but inline functions are normally reserved for very short functions, such as one-line "getters" or "setters" (although they in turn raise interesting OO-design questions). Your function is small, but best to form good habits now. In addition, most of the time, event-handling code should propagate the event after you do your custom work by including a call to the parent class code, as follows (for this case):

    Qt Code:
    1. ...
    2. QLabel::mousePressEvent(event);
    3. } // end function
    To copy to clipboard, switch view to plain text mode 

    This may or may not be what you want but is *usually* appropriate.

Similar Threads

  1. Replies: 2
    Last Post: 23rd November 2009, 14:03
  2. Replies: 1
    Last Post: 29th September 2009, 19:44
  3. Replies: 1
    Last Post: 2nd August 2008, 15:46
  4. [SOLVED] subclassing qlabel
    By mickey in forum Newbie
    Replies: 10
    Last Post: 4th June 2008, 14:43
  5. Subclassing
    By joseph in forum Newbie
    Replies: 1
    Last Post: 25th February 2006, 14:06

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.