Results 1 to 10 of 10

Thread: Click on QLabel

  1. #1
    Join Date
    Apr 2012
    Posts
    39
    Thanks
    7

    Exclamation Click on QLabel

    Hello!

    Some can tell me or provide a code that allows click on top of a QLabel and then do a certain action?

    Regards,
    Daniel Sousa

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Click on QLabel

    A way:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. {
    3. QLabel* l = new QLabel( "test", this );
    4. l->installEventFilter( this );
    5. }
    6.  
    7. bool MainWindow::eventFilter( QObject* object, QEvent* event )
    8. {
    9. if( event->type() == QEvent::MouseButtonRelease )
    10. {
    11. qDebug() << "Label action!"; // do what you want here
    12. }
    13.  
    14. return QMainWindow::eventFilter( object, event );
    15. }
    To copy to clipboard, switch view to plain text mode 

    A better way:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. {
    3. QPushButton* button = new QPushButton( "test", this );
    4. connect( button, SIGNAL( clicked() ), this, SLOT( buttonClicked() ) );
    5. }
    6.  
    7. void MainWindow::buttonClicked( void )
    8. {
    9. qDebug() << "Action!"; // do what you want here
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2012
    Posts
    39
    Thanks
    7

    Default Re: Click on QLabel

    Quote Originally Posted by Spitfire View Post
    A way:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. {
    3. QLabel* l = new QLabel( "test", this );
    4. l->installEventFilter( this );
    5. }
    6.  
    7. bool MainWindow::eventFilter( QObject* object, QEvent* event )
    8. {
    9. if( event->type() == QEvent::MouseButtonRelease )
    10. {
    11. qDebug() << "Label action!"; // do what you want here
    12. }
    13.  
    14. return QMainWindow::eventFilter( object, event );
    15. }
    To copy to clipboard, switch view to plain text mode 

    A better way:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. {
    3. QPushButton* button = new QPushButton( "test", this );
    4. connect( button, SIGNAL( clicked() ), this, SLOT( buttonClicked() ) );
    5. }
    6.  
    7. void MainWindow::buttonClicked( void )
    8. {
    9. qDebug() << "Action!"; // do what you want here
    10. }
    To copy to clipboard, switch view to plain text mode 
    This actually works but that's not what I want. I want each QLabel has a different behavior, but without the need to create a class for each QLabel.

  4. #4
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Click on QLabel

    I've done something, I'll send you an email with the solution.
    Regards

  5. #5
    Join Date
    Apr 2012
    Posts
    39
    Thanks
    7

    Default Re: Click on QLabel

    Quote Originally Posted by plopes21 View Post
    I've done something, I'll send you an email with the solution.
    Regards
    Not work. Please help.

  6. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Click on QLabel

    So, what's the problem with event filter?
    You can recognise which label triggered the event and do something based on that.

    Or, create ClickableLabel, a class that will emitt a 'clicked()' signal which you can connect to a slot that does something.
    Each label can be connected to different slot depending on what you need it to do:

    Qt Code:
    1. class ClickableLabel : public QLabel
    2. {
    3. Q_OBJECT
    4. public:
    5. ClickableLabel( QWidget* parent = 0 ) : QLabel( parent ) {}
    6.  
    7. signals:
    8. void clicked( void );
    9.  
    10. protected:
    11. void mouseReleaseEvent( QMouseEvent* event ) // or double clicked or what you want
    12. {
    13. QLabel::mouseReleaseEvent( event );
    14. emit this->clicked();
    15. }
    16. };
    To copy to clipboard, switch view to plain text mode 
    Single class and problem solved.


    Added after 7 minutes:


    Quote Originally Posted by plopes21 View Post
    I've done something, I'll send you an email with the solution.
    Regards
    Don't be shy. Show your solution to everyone so others may benefit in a future.
    Or if it's a bad idea you may get some feedback and learn something.
    Last edited by Spitfire; 2nd May 2012 at 14:30.

  7. #7
    Join Date
    Jul 2014
    Posts
    46
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Click on QLabel

    Hello I am new to QT. I can implement the signal and slot part but how do i know which label triggered the event?

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Click on QLabel

    Quote Originally Posted by antweb View Post
    Hello I am new to QT. I can implement the signal and slot part but how do i know which label triggered the event?
    A lot of options:

    - send some kind of identifier as a signal argument
    - use QSignalMapper
    - use QObject::sender() in the receiving slot
    - send a pointer to the label itself as a signal argument

    Cheers,
    _

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

    antweb (1st July 2014)

  10. #9
    Join Date
    Jul 2014
    Posts
    46
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Click on QLabel

    Thanks for the quick response.
    Like i have a number of QLabels and i want to see through which i got the click, can i differentiate on the basis of their names or should i check out the mouse position at that time.
    Can you please tell how i can do that?

  11. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Click on QLabel

    You don't have to check mouse position since each label will only emit its signal when it is clicked and not when another label is clicked.

    The rest is one of the option I've listed above.

    Cheers,
    _

Similar Threads

  1. Click on a qlabel
    By eltecprogetti in forum Qt Programming
    Replies: 1
    Last Post: 6th March 2012, 12:29
  2. Replies: 1
    Last Post: 7th May 2011, 20:24
  3. Replies: 1
    Last Post: 29th September 2009, 19:44
  4. Replies: 6
    Last Post: 5th June 2009, 09:38
  5. Replies: 2
    Last Post: 11th January 2009, 23:24

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.