[Qt-interest] Need to show a dialog on click of QLabel (Scott Aron Bloom)
John McClurkin
jwm at nei.nih.gov
Fri May 15 16:24:25 CEST 2009
Scott Aron Bloom wrote:
>> Kamakshi Sivaraman wrote:
>>> Hi All,
>>>
>>> Thanks for your valuable suggestions it worked well.
>>> But now I had another problem, I have around 300 - 500 labels,each
> has
>>> click event and the click event working well,
>>> but all the labels are stored in array of QLabel. Now while clicking
> I
>>> want to know that the click event is generated by which
> label-object.
>>> After clicking the label, in the slot function I unable to
> recognize,
>>> from which label-object the click is generated.
>>> Do we have any provisions with QT that recognizes inside the slot
>>> function that which label-object
>>> is actually clicked like- storing the address of label-object etc.
>>> Please suggest some thing upon this.
>>>
>> There are two possible solutions, both of which require only a single
> slot.
>> First, use QSignalMapper. The argument in the slot will be the
> index of
>> the button that sent the signal. Read about this in the documentation.
>> Second, in the slot get a pointer to the button that sent the
> signal
>> with the statements:
>> QButton *but = qobject_cast<QButton *>(sender());
>> if(but != (QButton *)NULL) {
>> code
>> }
>> It is important to use qobject_cast to cast the sender of the signal
> as
>> a type QButton. That way, if the sender happens not to be a QButton,
> the
>> cast will return NULL and you won't execute statements that might
> cause
>> a crash.
>
> ----------------
> The sender() wont work, because the sender is the SignalMapper, not the
> button.
>
> Simply connect to the signal mappers mapped( QWidget * widget ) signal..
> The widget sent back will be the button sent.. Then you can use
> qobject-cast
>
I didn't mean to suggest that the OP use the sender method with
QSignalMapper, rather that the sender method was an alternative to the
QSignalMapper method. The QSignalMapper method is limited in that it can
only use parameterless signals and slots. If you need the parameters in
the signals which are connected to a slot, then you need to use the
sender method.
More information about the Qt-interest-old
mailing list