What would be object name which sends event (Solved)
-
wrote on 16 Dec 2010, 12:52 last edited by
When we hit a button , it will call slot accordingly. Now I want to know the objectname of that button in the slot.
These are dynamics buttons with custom names.
-
wrote on 16 Dec 2010, 12:54 last edited by
you can use QObject::sender() to get the sending object. There you can query for the object name:
@
void myslot()
{
QObject* pObject = sender();
QString name = pObject->objectName();
}
@ -
wrote on 16 Dec 2010, 12:55 last edited by
Use QObject::sender() inside your slot to get a pointer to the emitter object.
-
wrote on 16 Dec 2010, 13:00 last edited by
Be aware, that the object returned by sender() is only valid if the slot is called via signal/slot connection (using QObject::connect() method). When the slot is called directly, sender() returns a null pointer.
-
wrote on 16 Dec 2010, 15:17 last edited by
Thank you Gerolf Reinwardt
Yes Volker it's via signal/slot
-
wrote on 16 Dec 2010, 15:39 last edited by
Good. Nontheless, I'd put a check if(pObject) { ... } around the code, just to be on the save side.
-
wrote on 16 Dec 2010, 15:46 last edited by
Ok nice. Now I'll also :)
-
wrote on 16 Dec 2010, 15:46 last edited by
I wanted to keep it as small as possible, but I would also do it :-)
-
wrote on 16 Dec 2010, 15:50 last edited by
To save you a line :-) you can write this:
@
void myslot()
{
if(QObject* pObject = sender()) {
QString name = pObject->objectName();
}
}
@ -
wrote on 16 Dec 2010, 16:03 last edited by
QSignalMapper might be an option for you, too.
-
wrote on 17 Dec 2010, 04:31 last edited by
It's rocking guys
Nice one Volker :)[quote author="Gerolf Reinwardt" date="1292514405"]I wanted to keep it as small as possible, but I would also do it :-)[/quote]
Totally agreed with you :)
-
wrote on 17 Dec 2010, 04:33 last edited by
[quote author="Tobias Hunger" date="1292515404"]QSignalMapper might be an option for you, too.[/quote]
Let me check this out.
6/12