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?

How to determine if signal is already connected?

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 7 Posters 14.8k Views 3 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.
  • 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 S 2 Replies 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 Online
                    Christian EhrlicherC Online
                    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
                    • 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.

                      S Offline
                      S Offline
                      Szamo
                      wrote last edited by
                      #21

                      @aleksejs
                      Borland made it simple and thus easy to use. "Simple" means that 1 signal could have 0 or 1 slot (button->OnClick is the signal, buttonClickHandler is the slot). If one wanted to connect more slots to 1 signal then had to implement handling more slots (listeners) in the 1 connected slot. For a simple GUI 1 slot was enough of course. "Simple" also means that threads were not considered at all, it could not call a slot in another thread like Qt can. It simply called the slot and if more threads were used then one had to implement handling race conditions (using mutex or such).
                      @JonB
                      Borland extended C++ introducing __closure keyword by which a class instance pointer (this) and a member method pointer could be bound together. Thus a function pointer could be used for a non member or a member method. Thus it does not matter if buttonClickHandler is a method in a class.

                      JonBJ 1 Reply Last reply
                      0
                      • S Szamo

                        @aleksejs
                        Borland made it simple and thus easy to use. "Simple" means that 1 signal could have 0 or 1 slot (button->OnClick is the signal, buttonClickHandler is the slot). If one wanted to connect more slots to 1 signal then had to implement handling more slots (listeners) in the 1 connected slot. For a simple GUI 1 slot was enough of course. "Simple" also means that threads were not considered at all, it could not call a slot in another thread like Qt can. It simply called the slot and if more threads were used then one had to implement handling race conditions (using mutex or such).
                        @JonB
                        Borland extended C++ introducing __closure keyword by which a class instance pointer (this) and a member method pointer could be bound together. Thus a function pointer could be used for a non member or a member method. Thus it does not matter if buttonClickHandler is a method in a class.

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote last edited by
                        #22

                        @Szamo
                        Well I cannot comment if Borland C++ required an extension to C++ to achieve something. That is not the way to go at least these days. And I don't think it was available on non-Windows platforms.

                        The restrictions you describe about 1 signal -> 1 slot would limit me compared to Qt. I would have to implement the multi-slot-handling queue. Similarly if I wanted to use threads. So of course Borland's code looks "simpler", because it is simpler and may require you to do more coding.

                        For my own part I don't find Qt's signals/slots "cumbersome". If someone likes the syntax of button->OnClick = buttonClickHandler rather than the connect(....) statements look at Python/PySide/PyQt which implements a style of button.onClick.connect(handler) instead. So the detail of connecting is "syntactic sugar", one could implement some kind of Borland/Python-style wrapper if wanted.

                        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