Results 1 to 2 of 2

Thread: How to emit a signal from a static function

  1. #1
    Join Date
    Jan 2020
    Qt products

    Default How to emit a signal from a static function

    I have been trying to emit a signal from a static function, but get an error message:
    error: cannot call member function ‘void MyClass::myStaticFunction()’ without object
    emit connectionChanged();
    The function myStaticFunction() is declared as static because it is a callback function from some processing interface. I have verified that when I do not try to emit a signal myStaticFunction() is duly called. MyClass is derived from QObject:

    class MyClass : public QObject

    I presume there must be a (simple?) solution to this?

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 864 Times in 851 Posts
    Qt products

    Default Re: How to emit a signal from a static function

    Signals must be declared using the "signals:" syntax in your class declaration. You didn't show anything really to tell us how you declared it.

    Signals must be emitted through an instance of the class that declares them. So I do not think there is a way that you can invoke "emit' via a static method. The corresponding slot must be able to resolve the instance that sent the signal (via QObject::sender()) and with a static method, there is no sender.

    Maybe if you showed a bit more context about what you are trying to do, we could provide some better advice. There are probably tricks you can use to map a static callback onto a member function of a class instance.

    One way I can think of is to make your callback function a member of a struct that also contains a static member that points to a QObject class instance. Signals are just ordinary C++ methods, and "emit" pretty much maps to "this->", so in your static callback method in the struct you could simulate the emit in this way:

    Qt Code:
    1. struct CallbackForwarder
    2. {
    3. static void myCallback()
    4. {
    5. if ( myInstance )
    6. myInstance->doCallback();
    7. }
    9. static MyClass * myInstance = nullptr;
    10. }
    To copy to clipboard, switch view to plain text mode 

    and you would pass &CallbackForwarded:: myCallback as the address of your callback function to your processing interface. Depending on that interface, you might also be able to define the callback as a functor (operator()()) or as a lambda.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. how to emit signal in a static function ?
    By cxl2253 in forum Qt Programming
    Replies: 32
    Last Post: 7th July 2016, 21:36
  2. How to emit a signal passed to a function
    By scieck in forum Qt Programming
    Replies: 4
    Last Post: 10th March 2015, 21:12
  3. How to emit signal from static callback function
    By blizniak83 in forum Qt Programming
    Replies: 3
    Last Post: 14th April 2012, 08:56
  4. How to emit signal from static method?
    By kusumat in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2011, 09:17
  5. Emit signal from const function
    By waynew in forum Qt Programming
    Replies: 9
    Last Post: 22nd August 2010, 13:10


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.