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. How to determine if signal is already connected?
Forum Updated to NodeBB v4.3 + New Features

How to determine if signal is already connected?

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 6 Posters 12.9k Views 2 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.
  • SPlattenS SPlatten

    @J-Hilk , perhaps I haven't made myself clear, I have two widgets that share spinners and a slider, in the constructor of the same two widgets I want to check if the spinners and slider have already had a signal connected, if the answer is no then I want to go ahead with the connection.

    Pl45m4P Offline
    Pl45m4P Offline
    Pl45m4
    wrote on last edited by
    #11

    @SPlatten said in How to determine if signal is already connected?:

    I have two widgets that share spinners and a slider, in the constructor of the same two widgets I want to check if the spinners and slider have already had a signal connected, if the answer is no then I want to go ahead with the connection.

    But why? You wrote the code. If you haven't connected it already, it's probably not connected :) And if you did, it should stay connected unless you disconnect it manually somewhere, somehow.

    Don't quite unterstand the issue you have here :)


    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    SPlattenS 1 Reply Last reply
    0
    • Pl45m4P Pl45m4

      @SPlatten said in How to determine if signal is already connected?:

      I have two widgets that share spinners and a slider, in the constructor of the same two widgets I want to check if the spinners and slider have already had a signal connected, if the answer is no then I want to go ahead with the connection.

      But why? You wrote the code. If you haven't connected it already, it's probably not connected :) And if you did, it should stay connected unless you disconnect it manually somewhere, somehow.

      Don't quite unterstand the issue you have here :)

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #12

      @Pl45m4 , the issue is I have two instances of the same widget, there are three common widgets for setting attributes on instances of the two, in the constructor of the widget I want to automatically check the shared widgets and then only connect if they haven't already been connected.

      The question I am asking is can I detected using the Qt library calls if a signal has already been connected to a widget.

      Kind Regards,
      Sy

      Pl45m4P 1 Reply Last reply
      0
      • SPlattenS SPlatten

        @Pl45m4 , the issue is I have two instances of the same widget, there are three common widgets for setting attributes on instances of the two, in the constructor of the widget I want to automatically check the shared widgets and then only connect if they haven't already been connected.

        The question I am asking is can I detected using the Qt library calls if a signal has already been connected to a widget.

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #13

        @SPlatten

        Besides calling connect again and check its output, there is

        int QObject::receivers(const char *signal)

        • https://doc.qt.io/qt-5/qobject.html#receivers

        Dont know if it's recommended to use...

        Warning: This function violates the object-oriented principle of modularity. However, it might be useful when you need to perform expensive initialization only if something is connected to a signal.

        I guess not, but maybe it helps :)


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        SPlattenS 1 Reply Last reply
        1
        • Pl45m4P Pl45m4

          @SPlatten

          Besides calling connect again and check its output, there is

          int QObject::receivers(const char *signal)

          • https://doc.qt.io/qt-5/qobject.html#receivers

          Dont know if it's recommended to use...

          Warning: This function violates the object-oriented principle of modularity. However, it might be useful when you need to perform expensive initialization only if something is connected to a signal.

          I guess not, but maybe it helps :)

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #14

          @Pl45m4 , doesn't calling connect just create another connection? Thats where I am now.

          Kind Regards,
          Sy

          J.HilkJ 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @Pl45m4 , doesn't calling connect just create another connection? Thats where I am now.

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

            @SPlatten
            @Pl45m4 is on the right track here,

            you can/should subclass your spinner and create a function,

            bool isValueChanged(){
               static const QMetaMethod valueChangedSignal = QMetaMethod::fromSignal(&MyObject::valueChanged);
            return isSignalConnected(valueChangedSignal);
            }
            

            https://doc.qt.io/qt-5/qobject.html#isSignalConnected


            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.

            SPlattenS Pl45m4P 2 Replies Last reply
            0
            • J.HilkJ J.Hilk

              @SPlatten
              @Pl45m4 is on the right track here,

              you can/should subclass your spinner and create a function,

              bool isValueChanged(){
                 static const QMetaMethod valueChangedSignal = QMetaMethod::fromSignal(&MyObject::valueChanged);
              return isSignalConnected(valueChangedSignal);
              }
              

              https://doc.qt.io/qt-5/qobject.html#isSignalConnected

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #16

              @J-Hilk , thank you, penny has dropped, I see what I have to do now.

              Kind Regards,
              Sy

              1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @SPlatten
                @Pl45m4 is on the right track here,

                you can/should subclass your spinner and create a function,

                bool isValueChanged(){
                   static const QMetaMethod valueChangedSignal = QMetaMethod::fromSignal(&MyObject::valueChanged);
                return isSignalConnected(valueChangedSignal);
                }
                

                https://doc.qt.io/qt-5/qobject.html#isSignalConnected

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #17

                @J-Hilk

                lol, found receivers() and sender() but not isSignalConnected().

                Sometimes it's hard to find something, if you don't know what you are looking for :D
                (function names)


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                2
                • A Offline
                  A Offline
                  aleksejs
                  wrote on last edited by aleksejs
                  #18

                  I'm working with Qt not so long, but I see how cumbersome is signal-slot mechanism in comparison to Borland Event-Handler mechanism already.
                  In Borland you write

                  button->OnClick = buttonClickHandler;
                  

                  or

                  if( !button->OnClick )
                      button->OnClick = buttonClickHandler;
                  

                  Until now I haven't seen better Event mechanism than Borland's.

                  JonBJ 1 Reply Last reply
                  0
                  • A aleksejs

                    I'm working with Qt not so long, but I see how cumbersome is signal-slot mechanism in comparison to Borland Event-Handler mechanism already.
                    In Borland you write

                    button->OnClick = buttonClickHandler;
                    

                    or

                    if( !button->OnClick )
                        button->OnClick = buttonClickHandler;
                    

                    Until now I haven't seen better Event mechanism than Borland's.

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

                    @aleksejs
                    How did that work/what did you specify when buttonClickHandler is a method in a class?

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @aleksejs
                      How did that work/what did you specify when buttonClickHandler is a method in a class?

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by Christian Ehrlicher
                      #20

                      ... Or if two distinct functions should be called where none is aware of the other?

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      0
                      • W wasawi2 referenced this topic on

                      • Login

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