Results 1 to 12 of 12

Thread: Clickable Qlabel

  1. #1
    Join Date
    Jul 2006
    Posts
    26
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Clickable Qlabel

    Hi, I want to add the clicked() signal to a QLabel.

    Can any1 point me the right direction to take?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Clickable Qlabel

    Override QWidget::mousePressEvent() and/or QWidget::mouseReleaseEvent().
    Emit the signal whenever you feel it's appropriate..
    J-P Nurmi

  3. #3
    Join Date
    Jul 2006
    Posts
    26
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Clickable Qlabel



    Im not sure I understand what you mean.

    How and where can i override that event?

    Is that a virtual function?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Clickable Qlabel

    Quote Originally Posted by lewis
    Hi, I want to add the clicked() signal to a QLabel.

    Can any1 point me the right direction to take?
    You can copy the code from wwActiveLabel. It should work fine without any changes (maybe one change is required, as widgets in Qt4 don't take the name argument in their constructors anymore).

  5. #5
    Join Date
    Jul 2006
    Posts
    26
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Clickable Qlabel

    Ill use that, but only if Im not able to make it myself quickly enough... I'd like to learn how to , thx tho

  6. #6
    Join Date
    Jul 2006
    Posts
    26
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Clickable Qlabel

    I get errors like these when using wwActiveLabel:

    error C2027: use of undefined type 'QMouseEvent': see declaration of 'QMouseEvent'
    error C2227: left of '->button' must point to class/struct/union/generic type

    if i include QMouseEvent, i get linking errors.

    im using qt 4.1.4 commercial
    Last edited by lewis; 18th July 2006 at 20:03.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Clickable Qlabel

    What kind of errors do you get?

    This compiles fine:

    Qt Code:
    1. /***************************************************************************
    2.  * Copyright (C) 2004 by Witold Wysota *
    3.  * wysota@wysota.eu.org *
    4.  * *
    5.  ***************************************************************************/
    6. #ifndef WWCLASSES_H
    7. #define WWCLASSES_H
    8.  
    9. #include <QLabel>
    10. #include <QTimer>
    11. #include <QPoint>
    12.  
    13. /**
    14.  * @class wwActiveLabel
    15.  * @brief Label with mouse click and mouse move support
    16.  *
    17.  *
    18.  */
    19. class wwActiveLabel : public QLabel
    20. {
    21. Q_OBJECT
    22. public:
    23. wwActiveLabel(QWidget *parent = 0); ///< default constructor
    24. ~wwActiveLabel();
    25. signals:
    26. void doubleClicked(); ///< double click with LMB
    27. void clicked(); ///< single click with LMB
    28. void pressed(); ///< LMB pressed
    29. void released(); ///< LMB released
    30. void mouseOver(); ///< mouse pointer just entered the widget
    31. void mouseOut(); ///< mouse pointer just left the widget
    32. protected:
    33. void mouseDoubleClickEvent ( QMouseEvent * e );
    34. void mousePressEvent( QMouseEvent *e);
    35. void mouseReleaseEvent( QMouseEvent *e);
    36. void enterEvent(QEvent *);
    37. void leaveEvent(QEvent *);
    38. bool hitLabel(const QPoint &p);
    39. bool m_pressed;
    40. };
    41.  
    42. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ***************************************************************************
    2. * Copyright (C) 2004 by Witold Wysota *
    3. * wysota@wysota.eu.org *
    4. * *
    5. ***************************************************************************/
    6. #include "wwclasses.h"
    7. #include <QMouseEvent>
    8.  
    9. wwActiveLabel::wwActiveLabel(QWidget *parent) : QLabel(parent) {
    10. m_pressed = false;
    11. setText("wwActiveLabel");
    12. }
    13. wwActiveLabel::~wwActiveLabel() {}
    14. void wwActiveLabel::mouseDoubleClickEvent( QMouseEvent * e ) {
    15. if(e->button() == Qt::LeftButton)
    16. emit doubleClicked();
    17. QLabel::mouseDoubleClickEvent(e);
    18. }
    19. void wwActiveLabel::mousePressEvent( QMouseEvent * e ) {
    20. if(e->button() == Qt::LeftButton) {
    21. m_pressed = true;
    22. } else
    23. m_pressed = false;
    24. emit pressed();
    25. }
    26. void wwActiveLabel::mouseReleaseEvent( QMouseEvent * e ) {
    27. if(m_pressed && e->button() == Qt::LeftButton && hitLabel(e->pos()))
    28. emit clicked();
    29. m_pressed = false;
    30. emit released();
    31.  
    32. }
    33. void wwActiveLabel::enterEvent( QEvent *e ) {
    34. QLabel::enterEvent(e);
    35. emit mouseOver();
    36. }
    37. void wwActiveLabel::leaveEvent( QEvent *e ) {
    38. QLabel::leaveEvent(e);
    39. emit mouseOut();
    40. }
    41. bool wwActiveLabel::hitLabel( const QPoint & p ){
    42. return rect().contains(p);
    43. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jul 2006
    Posts
    26
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Clickable Qlabel

    Of course i had linking errors, i forgot to add it to my .pro file and regenerate the project file!!! dumb me!

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Clickable Qlabel

    Quote Originally Posted by lewis
    Of course i had linking errors, i forgot to add it to my .pro file and regenerate the project file!!! dumb me!
    If you use wwActiveLabel in a commercial product, please put a copyright notice somewhere in its docs and/or in the "about" window (if your app has one).

  10. The following user says thank you to wysota for this useful post:

    lewis (18th July 2006)

  11. #10
    Join Date
    Jul 2006
    Posts
    26
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Clickable Qlabel

    its not for commercial use, its only a little educationnal game that will be used inside a small non-profit organization.

    Thx for ur wwActiveLabel widget!

  12. #11

    Default Re: Clickable Qlabel

    Quote Originally Posted by lewis View Post
    Hi, I want to add the clicked() signal to a QLabel.

    Can any1 point me the right direction to take?

    Hi Lewis, this is Oleg S, I have the same problem that you, how did you solve it?

    These are the messages errors:
    error C2027: use of undefined type 'QMouseEvent'
    c:\qt4.4.0\src\gui\kernel\qwidget.h(77) : see declaration of 'QMouseEvent'


    error C2227: left of '->pos' must point to class/struct/union/generic type
    Thanks so much!!
    Last edited by jpn; 4th June 2008 at 10:39. Reason: [quote] tags

  13. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Clickable Qlabel

    Oleg S,
    Qt Code:
    1. #include <QMouseEvent>
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. [SOLVED] subclassing qlabel
    By mickey in forum Newbie
    Replies: 10
    Last Post: 4th June 2008, 14:43
  2. QT4 layout of complex dialog is very slow
    By cboles in forum Qt Programming
    Replies: 15
    Last Post: 28th April 2006, 19:57
  3. how to catch enter press in QLabel?
    By Morea in forum Newbie
    Replies: 2
    Last Post: 26th March 2006, 22:53
  4. table in QLabel
    By Pan Wojtas in forum Qt Programming
    Replies: 19
    Last Post: 13th February 2006, 11:37
  5. QLabel with HTML-style formatting docs?
    By Everall in forum Qt Programming
    Replies: 6
    Last Post: 7th February 2006, 20:01

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.