Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, December 2007
how to find the caller in a function ?


Message 1 in thread

hello,

having a global function "A()" that will be called by different QObject 
derived objects,
how in "A()" to get the pointer the QObject* to the caller ?

is there a way to do this ?

thanks

--
 [ signature omitted ] 

Message 2 in thread

Not without passing the pointer to the function as a parameter. If you 
do "A(QObject* _caller)" then _caller will point to the QObject of the 
calling class.

- Thomas

Yong Taro schrieb:
> hello,
> 
> having a global function "A()" that will be called by different QObject 
> derived objects,
> how in "A()" to get the pointer the QObject* to the caller ?
> 
> is there a way to do this ?
> 
> thanks
> 
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 
> 

--
 [ signature omitted ] 

Message 3 in thread

If you use the signal/slot mechanism, you can use the function
QObject::sender()

http://doc.trolltech.com/4.3/qobject.html#sender

Greetings

Niklas

Yong Taro schrieb:
> hello,
> 
> having a global function "A()" that will be called by different QObject
> derived objects,
> how in "A()" to get the pointer the QObject* to the caller ?
> 
> is there a way to do this ?
> 
> thanks
> 
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ] 

Message 4 in thread

yes thanks, I was hoping somebody discovered some meta features that I'm 
not aware of.

--
 [ signature omitted ] 

Message 5 in thread

> how in "A()" to get the pointer the QObject* to the caller ?

This violates the principles of OO coding and modularity.  The function
should behave the same way regardless of who called it.  The problem comes
when a non-QObject makes a method call to your method.  You can't guarantee
it won't be the case if you want modularity.

If you really need to know the caller's QObject* address then pass it as an
argument. 

> is there a way to do this ?

In a slot you can use QObject::sender() or you _could_ walk the stack if you
are not in a slot.  Remember walking the stack is messy and prone to error! 
See above.

A

--
 [ signature omitted ]