Code for Click on QLabel!?
-
You can use "eventFilter":http://doc.qt.nokia.com/latest/qobject.html#eventFilter for this.
-
You can create a class that inherits from QLabel which will implement the following protected function:
@
signals:
void clicked();
protected:
void mousePressEvent ( QMouseEvent * evt);
void mouseReleaseEvent ( QMouseEvent * evt);
@You could set it to emit a clicked signal every time mouseReleaseEvent function is called.
-
You shoud ceate a SLOT to receive the signal from your QLabel. Try this:
@
connect(your_label, SIGNAL(clicked()), this, SLOT(onClicked()));
@ -
Sorry, my mistake. But, as fluca said you can use a pushbutton and change the appearance through QSS.
[quote author="Rahul Das" date="1323084178"]There is no clicked signal in QLabel.[/quote]
-
"Here":http://developer.qt.nokia.com/wiki/How_do_Qlabel_be_clickable you can find the source for 'click'able QLabel
//Off Topic : edit link of previous post is not working for me. :(
Anybody has the same problem ? -
You have the option to make it emit the clicked signal either when mouse gets pressed or when mouse gets release (i prefer the second one). Whatever feels better to you.
-
yea, and there is a "how to":http://developer.qt.nokia.com/wiki/How_do_Qlabel_be_clickable / wiki page to do this saying the same as favoritas37 said :)
[quote author="favoritas37" date="1323012419"]You can create a class that inherits from QLabel which will implement the following protected function:
@
signals:
void clicked();
protected:
void mousePressEvent ( QMouseEvent * evt);
void mouseReleaseEvent ( QMouseEvent * evt);
@You could set it to emit a clicked signal every time mouseReleaseEvent function is called.[/quote]
-
Rahul, i hope you don't mind, i edited the wiki by removing the slot because it caused the signal to be emitted 2 times. And also i changed the name of the new class to ClickableLabel since this is how it is mentioned in the description. Is that ok?
-
Thanks, and thats perfect. :) Two times the same signal, was a careless part anyway.
[quote author="favoritas37" date="1323115942"]Rahul, i hope you don't mind, i edited the wiki by removing the slot because it caused the signal to be emitted 2 times. And also i changed the name of the new class to ClickableLabel since this is how it is mentioned in the description. Is that ok?[/quote]