[Qt-interest] static function emitting signal

Gabriel M. Beddingfield gabrbedd at gmail.com
Wed Mar 24 05:07:06 CET 2010


Hi Chandru,

On Wed, 24 Mar 2010, Chandru... wrote:

> hi friends,
> i am having an static call back member function in my QThread class which
> will be like our event handler for scanner device and in static void
> function how can i emit a signal to my base class GUI class..
>
> here in static function i canuse only static variable and functions so
> please suggest me a clear way to solve this problem ...

A lot of callback API's will do something like this:

     typedef void (*SomeCallbackType)(void* arg);
     int register_callback(SomeCallbackType function, void* arg);

the 'arg' is a handle to... anything you want.  When 
wrapping this sort of callback API in C++, it's common to do 
this:

class Foo {
public:
     Foo() {}

     static void _callback(void* arg) {
         Foo* that = static_cast<Foo*>(arg);
 	that->my_callback();
     }

protected:
     void my_callback() {
         // Do your real work here
     }
};

int main()
{
     Foo f;
     register_callback(Foo::_callback, &f);

     //...
}

However, if the API you're using doesn't allow you to pass 
an argument like this... you're going to have to use some 
manner of global variable to get back to your class 
instance.

HTH,
Gabriel



More information about the Qt-interest-old mailing list