Help with sender() analysis
-
I am trying to use sender() to find the actual "index" which triggered the connect.
I looked at the "RX_object" and see lot of info there but cannot find "index"..
The parameters pased to SLOT are max values , not actual "index".
Many thanks for help.connect(subMenu_checkableActionInMenu[index] , &QAction::triggered, this ,[=]() { this->processAction(index_sub,index) ;});
QObject *RX_object = sender();
-
@AnneRanch
You won't find the index there.
sender()
is aQObject
, which in your case is either aQMenu
or aQAction
.
If the sender of a signal needs to know certain properties, you can usesetProperty
, to store information and read it in the slot, see here. -
@Axel-Spoerl per doc
Advanced Signals and Slots Usage
For cases where you may require information on the sender of the signal, Qt provides the QObject::sender() function, which returns a pointer to the object that sent the signal.
so
how do I get that pointer ?
QObject *RX_object = sender(); ?and when I get it should it contain the "index" ?
-
@AnneRanch said in Help with sender() analysis:
and when I get it should it contain the "index" ?
No, it will never contain your
index
, because you added the structure ofQMenu
- andQAction
-items. The singleQActions
don't know about any index or what you've built around, so does their base classQObject
not know.
The index of the signal emitting widget in your container is neither transmitted nor can be somehow extracted from thesender()
QObject
.If you need the container index of your sender item in your slot, there is, for sure, a better solution to do what you want to do.
-
@AnneRanch
sender()
will in no way help you, and should be avoided.There are just two ways to pass values (like
index_sub
orindex
) from where you do theconnect()
to a slot connected to the signal:- Write the slot as a lambda, as we have discussed, just as your
connect(subMenu_checkableActionInMenu[index] , &QAction::triggered, this ,[=]() { this->processAction(index_sub,index) ;});
This, for example, passes the
index
as a parameter which is used insubMenu_checkableActionInMenu[index]
that is the sender. So it is just what you are attemtping to get out ofsender()
. If you do not think that has the right value then there is something mixed up.- Add the value as a property to the sending object, as @Axel-Spoerl wrote earlier, and then use
sender()
to get that object and read the stored property in it. Which is a long and complicated way round compared to using a lambda.
-
@JonB I do not think I said this - but the "index
passed is the index to LAST item in menu, NOT the current index which trigged the connect.
Yes , I must have something in code which is wrong , obviously ... I'll work on that. -
@AnneRanch
All I can say is if you try something like:// assume array has >= 3 elements for (int index = 0; index < 3; index++) connect(subMenu_checkableActionInMenu[index], &QAction::triggered, this, [=]() { qDebug() << index; } );
I would expect you to see 0/1/2 according as which one you click on. You might like to test that.