Caller function ID (or something to that tune)



  • I know this is not a Qt question but still

    I have a single instance of a class in my app (like singleton but actually declared as a global variable). Its member function gets called by instances of other classes (both different and same) in an arbitrary order through timers, and I want to distinguish between the callers, so the function would emit a signal with cashed results only if called by a new caller.

    I can easily do this via passing a parameter to the called function but I more like the sender() approach used for slots and signals, though the function is not called via a signal either directly or via another function (so sender() returns 0)

    Is it possible to do that in a simple way? I don't need function names or whatever, just a way to distinguish between the callers



  • @deisik
    Try passing 'this' as a parameter from calling function so that you will have object and you can assign it to Connect. Hope this helps.

    -Tirupathi



  • @deisik said in Caller function ID (or something to that tune):

    I can easily do this via passing a parameter to the called function

    That's the correct approach. "The sender() way" breaks OO principles and I would not be surprised if it was at least deprecated in Qt6



  • Actually, I didn't mean doing that by using the sender() option since it is not available in my case. If I'm not mistaken, you can check the stack with the backtrace() functions of execinfo.h. but it looks like a massive overkill as compared to simply passing this (or whatever) to the function directly

    Anyway, I have already worked around my problem by setting a counter in the resulting dataset which allows the callers (and their receiving slots) to decide whether the data have been updated and they actually need to process the data received. So no need to find out the caller


  • Moderators

    @deisik said in Caller function ID (or something to that tune):

    you can check the stack with the backtrace()

    This would be like shooting a sparrow with artillery :-)


  • Moderators

    QSignalMapper is deprecated, but might still be handy for your use case : https://doc.qt.io/qt-5/qsignalmapper.html


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.