Results 1 to 17 of 17

Thread: QLabel::clicked()

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 10: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

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.