[Qt-interest] Implement click event on QLabel

Andre Somers andre at familiesomers.nl
Tue Dec 7 17:15:01 CET 2010


Op Di, 7 december, 2010 4:59 pm, schreef Lucas P. Caixeta:
> Hello!
>
> Guys, i need to implement a click event on a QLabel...
>
> i 'm trying:
>
>     connect(ui->label_exit, SIGNAL(linkActivated(QString)), this
> ,close_event() );
>
>     void mainwindows_plus::close_event()
>
>     {
>
>         qDebug("foi!!!!!!!!!!!");
>
>     }
>
>
> But it dont run...=/
>
>
> How can i reimplement or use the click event on a QLabel??

A connect statement can only be used to connect a SIGNAL (good, you have
one) to a SLOT (which you don't have). So, to start, you should make
close_event a SLOT.
However, linkActivated is not a clicked event. It is exactly what the
names suggest: a _link_ inside the rich text content of the label widget
was clicked. That is not the same as a clicked event. If you need a real
clicked event, you can do two things:

1) Best: use a button. It was made to deliver clicked events. You can
style it to look the way you need, but if it is button functionality you
want, it is probably best to use a button.

2) Use events (that is different from signals! See QEvent) to do what you
need. Two ways for that:
2a) Subclass QLabel, and reimplement the mouse event handlers and declare
a clicked signal for your class. You can emit your clicked signal as a
response to the appropriate events. You can then connect to that clicked
signal like you can connect to any other signal to do whatever you need to
have done.
2b) Install an event filter on your QLabel, and do the handling of the
events from 2a in the class where you need to receive the click. It will
require subclassing then too, but perhaps you already are doing that.

André






More information about the Qt-interest-old mailing list