14

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

user99545
  • 1,173
  • 3
  • 16
  • 32
  • 1
    Where do you connect the SIGNAL and the SLOT? – Nobody moving away from SE Feb 23 '12 at 10:32
  • I connect them in a separate `.cpp` file that is called on a button press. `void MainWindow::on_pushButton_clicked() { tProc = new Foo(this); connect(tProc, SIGNAL(ValChanged(int)), this, SLOT(onNumberChanged(int))); tProc->start(); }` it then creates a new instance of my `Foo` class and starts the thread that calls the callback function. – user99545 Feb 23 '12 at 10:40
  • In your static callback function you create a new Foo instance and call the signal on it. I suppose this is the point where you want to emit the signal. How do you connect this instance to the slot you want to address? – Nobody moving away from SE Feb 23 '12 at 10:45
  • I am not completely sure what you mean, but I am assuming I connected it correctly in the `MainWindow::on_pushButton_clicked()` function. Do I need to somehow re-connect it in the static function? – user99545 Feb 23 '12 at 10:49
  • 2
    In `clicked` function you connected your slot to `one` `Foo`, but in `callback` you created another separate `Foo` that knows nothing about `first` foo. And you emit signal of this new `Foo`, but signals of new `Foo` are not connected to any slots. – Lol4t0 Feb 23 '12 at 10:56
  • Gotcha. It boils down to my first problem then. Calling the `emit` from my `static` callback function. The compiler won't let me call a non-static function (the emit) from a static function (callback). I tried to solve it by creating the new class. – user99545 Feb 23 '12 at 11:01

4 Answers4

13

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.

Joachim W
  • 7,290
  • 5
  • 31
  • 59
BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • This would work, but the problem is my static callback function is being called by another function that is from the libCURL library and I cannot pass arguments to it. I am not sure how to implement your method in this case. – user99545 Feb 23 '12 at 11:20
  • 1
    @user99545 add a static variable, which is used in the callback. I'll edit the answer – BЈовић Feb 23 '12 at 11:22
  • Thanks. I ended up using your static variable and initialized it by passing the current class instance as a parameter to the thread. I used that parameter to initialize the class and it worked. – user99545 Feb 23 '12 at 11:39
  • Thanks so much! I wasted hours on trying to get it to work, your second suggestion was great! – one May 26 '14 at 07:39
  • @BЈовић I tried the second approach but it says undefined reference to `Foo ::theFoo' when I want to initialize it in contractor. – Majid Hojati Jan 25 '18 at 11:15
  • @MajidHojati What is "contractor"? See https://stackoverflow.com/questions/4547660/c-static-member-variable-and-its-initialization – BЈовић Jan 25 '18 at 13:02
4

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);
   }
max
  • 146
  • 1
  • 8
3

There is an elegant solution. You can emit a signal in a static member function like this:

emit (instance().newMessage(message));
Louis Xu
  • 41
  • 1
  • 4
0

You must provide a pointer to the class instance in order to make it work.

Notice however that in general it is not advised to emit signals from static functions. Static functions can be called without creating an instance of the class, which means that (if you do not provide as argument the sender and use it explicitely), you will not have a "sender" object for the emited signals.

For these cases as Lol4t0 suggests I also prefer callbacks.

pnezis
  • 12,023
  • 2
  • 38
  • 38