I am trying to code a static callback function that is called frequently from another static function within the same class. My callback function needs to emit
a signal but for some reason it simply fails to do so. I have put it under a debugger and the slot
never gets called. However when I place the code I used to emit
the data in a non-static function it works. Is there a reason I cannot emit a signal from a static function? I have tried declaring a new instance of the class and calling the emit function but with no luck.
class Foo
{
signals:
emitFunction(int);
private:
static int callback(int val)
{
/* Called multiple times (100+) */
Foo *foo = new Foo;
foo.emitFunction(val);
}
void run()
{
callback(percentdownloaded);
}
};
I have posted some basic code that demonstrates what I am attempting to do. I will post full code upon request.
Edit: I am posting the full code since this is kind of an odd scenario. http://pastebin.com/6J2D2hnM
That is not going to work, because you are creating a new Foo every time you enter that static function, and you do not connect a signal to a slot.
So, the fix would be to pass the object to that function :
class Foo
{
signals:
emitFunction(int);
private:
static int callback(int val, Foo &foo)
{
/* Called multiple times (100+) */
foo.emitFunction(val);
}
void run()
{
callback(percentdownloaded, *this);
}
};
Another option is to use postEvent, but I wouldn't recommend it.
Since you can not modify callback's signature, you can do it like this :
class Foo
{
signals:
emitFunction(int);
private:
static int callback(int val)
{
/* Called multiple times (100+) */
theFoo->emitFunction(val);
}
static Foo *theFoo;
void run()
{
callback(percentdownloaded, *this);
}
};
but you'll have to initialize that static variable somewhere.
in case someone still finding the solution, here is what I did, it works in my project. 1. make your class to be a singleton 2. in static cb function , load emitFunction from the your singleton class
static int callback(int val)
{
/* Called multiple times (100+) */
MYClass::getInstance()->emitFunction(val);
}
There is an elegant solution. You can emit a signal in a static member function like this:
emit (instance().newMessage(message));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With