Results 1 to 17 of 17

Thread: QLabel::clicked()

  1. #1
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Red face QLabel::clicked()

    Hi,
    I am using QT 4.2.2. I have a label in my main window. I want a "QLabel::clicked()" signal for my label, ie, i want to call a function when i click on a lable. How can I do that.

  2. #2
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel::clicked()

    Quote Originally Posted by sabeesh View Post
    Hi,
    I am using QT 4.2.2. I have a label in my main window. I want a "QLabel::clicked()" signal for my label, ie, i want to call a function when i click on a lable. How can I do that.
    There is no clicked signal in QLabel. What you can do is create your own QLabel class and override mousePressEvent to fire a clicked signal and catch it in your defined slot.

  3. #3
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel::clicked()

    Hi,
    Not clear!!!!!!!!!!!

  4. #4
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel::clicked()

    Quote Originally Posted by sabeesh View Post
    Hi,
    Not clear!!!!!!!!!!!
    Qt Code:
    1. myLabel::myLabel( const QString & text, QWidget * parent )
    2. :QLabel(parent)
    3. {
    4. connect( this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) );
    5. }
    6. void myLabel::mousePressEvent ( QMouseEvent * event )
    7. {
    8. emit clicked();
    9. }
    10. void myLabel::slotClicked()
    11. {
    12. qDebug()<<"Clicked";
    13. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel::clicked()

    Hi,
    When I give this code, qt display some errors. Can you give me a small discreption aout this code. because, i am a new comer in QT!!!!!

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel::clicked()

    When I give this code, qt display some errors.
    How do you "give" the code?

    That is a QLabel subclass.
    You have to define your own class, and add those methods to it and also the signal.

    That code is no not complete. It is just a suggestion.
    You will have to embed it in your application.

    Regards

  7. #7
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel::clicked()

    Ok, here is the full code

    //.h
    Qt Code:
    1. class myLabel : public QLabel
    2. {
    3. Q_OBJECT
    4. public:
    5. myLabel( const QString & text, QWidget * parent = 0 );
    6. ~myLabel(){}
    7.  
    8. signals:
    9. void clicked();
    10.  
    11. public slots:
    12. void slotClicked();
    13.  
    14. protected:
    15. void mousePressEvent ( QMouseEvent * event ) ;
    16.  
    17. };
    18. #endif
    To copy to clipboard, switch view to plain text mode 

    .cpp
    Qt Code:
    1. myLabel::myLabel( const QString & text, QWidget * parent )
    2. :QLabel(parent)
    3. {
    4. connect( this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) );
    5. }
    6.  
    7. void myLabel::slotClicked()
    8. {
    9. qDebug()<<"Clicked";
    10. }
    11.  
    12. void myLabel::mousePressEvent ( QMouseEvent * event )
    13. {
    14. emit clicked();
    15. }
    To copy to clipboard, switch view to plain text mode 

    In your mainWindows.cpp in the constructor add this

    Qt Code:
    1. MainWindow::MainWindow(QWidget* parent)
    2. : QMainWindow(parent)
    3. {
    4. myLabel* m_label = new myLabel(this);
    5. }
    To copy to clipboard, switch view to plain text mode 

    I have not tested the code so please correct the code if you get any errors
    Thanks

  8. The following 6 users say thank you to vermarajeev for this useful post:

    antweb (1st July 2014), Fivezero05 (11th August 2016), gboelter (15th April 2010), gun4boy (11th October 2010), Jake123 (27th January 2013), sabeesh (2nd August 2007)

  9. #8
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    5

    Default Re: QLabel::clicked()

    Hi there!!

    And how is it possible for the Widget to catch the clicked signal from the label???

    Thanks for the help!!!

    Best regards!

  10. #9
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    1
    Qt products
    Qt4

    Default Re: QLabel::clicked()

    code of vermarajeev works well for me

  11. #10
    Join Date
    Sep 2010
    Location
    Germany
    Posts
    28
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QLabel::clicked()

    Quote Originally Posted by jbarrena View Post
    Hi there!!

    And how is it possible for the Widget to catch the clicked signal from the label???

    Thanks for the help!!!

    Best regards!

    MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    {
    myLabel* m_label = new myLabel(this);
    }
    If you use this example you can catch it like this:

    MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    {
    myLabel* m_label = new myLabel(this);
    connect(myLabel, SIGNAL(clicked()), SLOT(myActionWhenMyLabelHasBeenClicked()));
    }

  12. The following user says thank you to Sven for this useful post:

    Jake123 (27th January 2013)

  13. #11
    Join Date
    Sep 2010
    Posts
    62
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLabel::clicked()

    Hi,

    only one remark:

    The "real click" will be better simulated if you will use the
    mouseReleaseEvent(QMouseEvent *e) instead of mousePressEvent(QMouseEvent *e)


    regards

    me
    Last edited by lotek; 11th October 2010 at 11:35.

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

    Jake123 (27th January 2013)

  15. #12
    Join Date
    Oct 2010
    Location
    Dublin, Ireland
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel::clicked()

    I agree with Lotek.
    A mouse "click" is traditionally a full press-then-release event series.
    Interpreting a click on a mouse press disallows the user from canceling the click when they drag the mouse away from the item, amongst other things.
    Just my two-cents.
    doggy

  16. #13
    Join Date
    Jan 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel::clicked()

    And how to check which label has been clicked if have many????

  17. #14
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QLabel::clicked()

    Using above example if You connect multiple instances of myLabel object into one slot then within that slot You can use sender() to determine what myLabel object send the signal. To have full access You can cast it lik:

    Qt Code:
    1. myLabel *Label = qobject_cast<myLabel *>( this->sender() );
    2. Label->setText( QString( "This sender is: %1" ).arg( this->sender()->objectName() ) );
    To copy to clipboard, switch view to plain text mode 

    Label is pointer to the object that send signal.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  18. #15
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    1

    Default Re: QLabel::clicked()

    thanks to this thread! I'm having the same problem with my project and this works just fine for me. But, in my application, besides emitting the signal after the mouse is clicked/released, I also need to know the coordinates of the clicked point. Is there any way to program this??
    I'm new to Qt and is learning .

  19. #16
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLabel::clicked()

    Did you read the docs on QMouseEvent? There are 7 different methods to give you information about the position of the mouse.

    If the slot listening for the clicked() signal needs to know the mouse position, then change the clicked signal to send the position as an argument:

    Qt Code:
    1. // myLabel.h
    2. signals:
    3. void clicked( const QPos & );
    4.  
    5. // myLabel.cpp:
    6.  
    7. void myLabel::mouseReleaseEvent( QMouseEvent * event )
    8. {
    9. emit clicked( event->pos() );
    10. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  20. The following user says thank you to d_stranz for this useful post:

    study24816 (1st August 2012)

  21. #17
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    1

    Default Re: QLabel::clicked()

    I think I've foudn the answer. the pointer *event to QMouseEvent stores the relevant information about the event mousepress/release. I gotta read more about QMouseEvent.

    Thanks

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.