Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pass signal to a function and emit it

Pass signal to a function and emit it

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    federico.massimi
    wrote on last edited by
    #1

    hi, I wanted to know if it was possible to pass a signal to a function, let me explain better:
    it is possible to write such function in QT C++ (Pseudocode):

    bool MyClass::test(int value, .... my_signal) {
          if( value > 0 ) {
                  emit my_signal(value);
                  return true;
          } else return false;
    }
    

    And call it in this way

    test(value1, MyClass::my_signal_for_value1);
    test(value2, MyClass::my_signal_for_value2);
    

    Thanks in advance

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • F federico.massimi

      hi, I wanted to know if it was possible to pass a signal to a function, let me explain better:
      it is possible to write such function in QT C++ (Pseudocode):

      bool MyClass::test(int value, .... my_signal) {
            if( value > 0 ) {
                    emit my_signal(value);
                    return true;
            } else return false;
      }
      

      And call it in this way

      test(value1, MyClass::my_signal_for_value1);
      test(value2, MyClass::my_signal_for_value2);
      

      Thanks in advance

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @federico-massimi You could pass a lambda which emits the signal instead of passing signal.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • F federico.massimi

        hi, I wanted to know if it was possible to pass a signal to a function, let me explain better:
        it is possible to write such function in QT C++ (Pseudocode):

        bool MyClass::test(int value, .... my_signal) {
              if( value > 0 ) {
                      emit my_signal(value);
                      return true;
              } else return false;
        }
        

        And call it in this way

        test(value1, MyClass::my_signal_for_value1);
        test(value2, MyClass::my_signal_for_value2);
        

        Thanks in advance

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @federico-massimi
        since a signal is nothing but a function itself, you should be able to pass function pointers as arguments.

        Like one does in c++ :)

        take a look here for some hands on stuff
        https://stackoverflow.com/a/12662961


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4
          template <class T>
          bool test(int value, T my_signal) {
              if( value > 0 ) {
                  (this->*my_signal)(value);
                  return true;
              }
              return false;
          }
          

          Usage myObject.test(value1, &MyClass::my_signal_for_value1);

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          JonBJ 1 Reply Last reply
          6
          • VRoninV VRonin
            template <class T>
            bool test(int value, T my_signal) {
                if( value > 0 ) {
                    (this->*my_signal)(value);
                    return true;
                }
                return false;
            }
            

            Usage myObject.test(value1, &MyClass::my_signal_for_value1);

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @VRonin , @federico-massimi
            I like @VRonin's template-based definition above. In addition, if I understand rightly(?), his use of (this->*my_signal)(value) means that your slot can still access QObject::sender(), which can be useful.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved