View Full Version : QLabel::clicked()
sabeesh
2nd August 2007, 06:03
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.
vermarajeev
2nd August 2007, 06:36
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.
sabeesh
2nd August 2007, 06:45
Hi,
Not clear!!!!!!!!!!!
vermarajeev
2nd August 2007, 06:49
Hi,
Not clear!!!!!!!!!!!
myLabel::myLabel( const QString & text, QWidget * parent )
:QLabel(parent)
{
connect( this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) );
}
void myLabel::mousePressEvent ( QMouseEvent * event )
{
emit clicked();
}
void myLabel::slotClicked()
{
qDebug()<<"Clicked";
}
sabeesh
2nd August 2007, 07:02
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!!!!!
marcel
2nd August 2007, 10:46
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
vermarajeev
2nd August 2007, 13:26
Ok, here is the full code
//.h
class myLabel : public QLabel
{
Q_OBJECT
public:
myLabel( const QString & text, QWidget * parent = 0 );
~myLabel(){}
signals:
void clicked();
public slots:
void slotClicked();
protected:
void mousePressEvent ( QMouseEvent * event ) ;
};
#endif
.cpp
myLabel::myLabel( const QString & text, QWidget * parent )
:QLabel(parent)
{
connect( this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) );
}
void myLabel::slotClicked()
{
qDebug()<<"Clicked";
}
void myLabel::mousePressEvent ( QMouseEvent * event )
{
emit clicked();
}
In your mainWindows.cpp in the constructor add this
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
myLabel* m_label = new myLabel(this);
}
I have not tested the code so please correct the code if you get any errors
Thanks
vBulletin® v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.