PDA

View Full Version : Click on a qlabel



eltecprogetti
6th March 2012, 12:17
Hi,

I created an array of QLabel on my form:

QLabel * lblPulsante = new QLabel (this);
Pulsante[i]= lblPulsante;

Now, I need to intercept the events click on my QLabel, but I do not know how to do.

I suppose I must to add something in the:

private slots: (in the file mainwindow.h) but I don't know the sintax.

Help me please..........thanks

high_flyer
6th March 2012, 13:29
QLabel * lblPulsante = new QLabel (this);
Pulsante[i]= lblPulsante;

I take it that this code is part of a loop?
Using a container would be better and safer, such a QList or QVector.
But at all having an array of widgets in such as way is very suspicious.
Arrays are good for managing large sets - but widgets usually are not that many - so usually its better to have a variable per widget - and use a QList of pointers to these widgets in addition if you need to perform actions on all of them.

To your question:
You can either subclass your QLabel and overload the mouse events - or, you can install an event filter.
http://qt-project.org/doc/qt-4.8/qobject.html#id-92a80cca-a72f-446a-b82e-f62fd93bd477