Results 1 to 6 of 6

Thread: Subclass QLabel

  1. #1
    Join Date
    Aug 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Subclass QLabel

    I am new in QT...
    So, my question is clear for most of you, but I still dont understand,
    how can I subclass QLabel to make my own enterEvent, leaveEvent, mousePressEvent, mouseReleaseEvent, to extend QLabel functionality.
    And then use it in my .ui application?

  2. #2
    Join Date
    Aug 2011
    Location
    Seoul, Korea
    Posts
    46
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclass QLabel

    Quote Originally Posted by Globulus View Post
    I am new in QT...
    So, my question is clear for most of you, but I still dont understand,
    how can I subclass QLabel to make my own enterEvent, leaveEvent, mousePressEvent, mouseReleaseEvent, to extend QLabel functionality.
    And then use it in my .ui application?
    Hi there,

    I reply here instead of the previous post.

    Qt Code:
    1. class MyLabel : public QLabel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyLabel(QWidget * parent = 0);
    7. ~MyLabel();
    8.  
    9. protected slots:
    10.  
    11. virtual void enterEvent ( QEvent * event );
    12. virtual void leaveEvent ( QEvent * event );
    13. virtual void mouseMoveEvent ( QMouseEvent * event );
    14. virtual void mousePressEvent ( QMouseEvent * event );
    15. virtual void mouseReleaseEvent ( QMouseEvent * event );
    16. };
    17.  
    18. MyLabel::MyLabel(QWidget * parent) : QWidget(parent)
    19. {
    20. }
    21.  
    22. MyLabel::~MyLabel()
    23. {
    24. }
    25.  
    26. void MyLabel::enterEvent ( QEvent * event )
    27. {
    28. }
    29.  
    30. void MyLabel::leaveEvent ( QEvent * event )
    31. {
    32. }
    33.  
    34. void MyLabel::mouseMoveEvent ( QMouseEvent * event )
    35. {
    36. }
    37.  
    38. void MyLabel::mousePressEvent ( QMouseEvent * event )
    39. {
    40. }
    41.  
    42. void MyLabel::mouseReleaseEvent ( QMouseEvent * event )
    43. {
    44. }
    To copy to clipboard, switch view to plain text mode 

    In order to use this widget in the QtDesigner...

    1. remember the header file name of this class (i.e. MyLabel.h in this case)
    2. go to QtDesigner and put a QLabel or QWidget into the form
    3. and the promote the QLabel or QWidget to MyLabel.

    Hope this helps.

    Regards,
    Last edited by Dong Back Kim; 6th August 2011 at 17:24. Reason: updated contents
    Dong Back Kim

  3. The following user says thank you to Dong Back Kim for this useful post:

    Globulus (6th August 2011)

  4. #3
    Join Date
    Aug 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Subclass QLabel

    That was usefull!
    Thanks!

  5. #4
    Join Date
    Aug 2011
    Location
    Seoul, Korea
    Posts
    46
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclass QLabel

    Quote Originally Posted by Globulus View Post
    That was usefull!
    Thanks!
    My pleasure =)
    Dong Back Kim

  6. #5
    Join Date
    Jun 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Subclass QLabel

    cn u gv me complete code for this...
    ?

  7. #6
    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: Subclass QLabel

    That is "complete code for this." What is more, inheriting from a class and overriding or adding functions is pure C++ and not Qt specific.

Similar Threads

  1. subclass the QLabel
    By ready in forum General Programming
    Replies: 29
    Last Post: 26th March 2011, 11:08
  2. Replies: 8
    Last Post: 12th February 2010, 02:41
  3. Replies: 1
    Last Post: 29th September 2009, 19:44
  4. Replies: 1
    Last Post: 2nd August 2008, 15:46
  5. Subclass
    By merry in forum General Programming
    Replies: 2
    Last Post: 1st March 2007, 10:34

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.