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.1k 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 Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I have a few controls that are common to widgets, in the widget constructor I connect the shared control signals, what I need to do is determine if the signals have already been connected and only go ahead with the connect if no connection already exists, how can I do this?

    Kind Regards,
    Sy

    Pl45m4P 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I have a few controls that are common to widgets, in the widget constructor I connect the shared control signals, what I need to do is determine if the signals have already been connected and only go ahead with the connect if no connection already exists, how can I do this?

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

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

      how can I do this?

      By default, a signal is emitted for every connection you make; two signals are emitted for duplicate connections. You can break all of these connections with a single disconnect() call. If you pass the Qt::UniqueConnection type, the connection will only be made if it is not a duplicate. If there is already a duplicate (exact same signal to the exact same slot on the same objects), the connection will fail and connect will return an invalid QMetaObject::Connection.

      (https://doc.qt.io/qt-5/qobject.html#connect)

      Pass Qt::UniqueConnection to those which should be unique and check against invalid QMetaObject::Connection (return type of connect)


      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
      • J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        keep in mind, that Qt::UniqueConnection doesn't work in all places,

        Lambdas in particular and (potentially) across thread, as it will behave as Qt::AutoConnection


        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 1 Reply Last reply
        1
        • J.HilkJ J.Hilk

          keep in mind, that Qt::UniqueConnection doesn't work in all places,

          Lambdas in particular and (potentially) across thread, as it will behave as Qt::AutoConnection

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

          @J-Hilk , all of these connections are lambda. Any other suggestions?

          Kind Regards,
          Sy

          J.HilkJ 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @J-Hilk , all of these connections are lambda. Any other suggestions?

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

            @SPlatten
            ok, first of all, check it yourself, I may have outdated information :D

            2ndly, an easy workaround:

            static const bool myConnection = connect(sender, signal, lambda);
            qDebug() << "Connect was successful" << myConnection;
            

            this however will not work, when your sender becomes invalid during runtime of your program.
            In that case I would suggest a list/vector to keep track of your connects yourself
            🙈


            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 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @SPlatten
              ok, first of all, check it yourself, I may have outdated information :D

              2ndly, an easy workaround:

              static const bool myConnection = connect(sender, signal, lambda);
              qDebug() << "Connect was successful" << myConnection;
              

              this however will not work, when your sender becomes invalid during runtime of your program.
              In that case I would suggest a list/vector to keep track of your connects yourself
              🙈

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

              @J-Hilk , that's pretty much what I do already, however what I want to do is:

              1. Does "valueChanged" connection exist for spinner widget?
              2. No, connect signal

              Kind Regards,
              Sy

              J.HilkJ 1 Reply Last reply
              0
              • SPlattenS SPlatten

                @J-Hilk , that's pretty much what I do already, however what I want to do is:

                1. Does "valueChanged" connection exist for spinner widget?
                2. No, connect signal
                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @SPlatten can you give a bit more information/code sections?

                does the check Does "valueChanged" connection exist for spinner widget? happen often, or only after creation of the spinner widget?


                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 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @SPlatten can you give a bit more information/code sections?

                  does the check Does "valueChanged" connection exist for spinner widget? happen often, or only after creation of the spinner widget?

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

                  @J-Hilk , the Widget is actually QSpinBox and yes it comes up as soon as I type QSpinBox::, not sure if I misunderstood you, I have two instances of the same widget on a display that shave the result of the spinner, its the constructor of this widget that connects the signals.

                  Kind Regards,
                  Sy

                  J.HilkJ 1 Reply Last reply
                  0
                  • SPlattenS SPlatten

                    @J-Hilk , the Widget is actually QSpinBox and yes it comes up as soon as I type QSpinBox::, not sure if I misunderstood you, I have two instances of the same widget on a display that shave the result of the spinner, its the constructor of this widget that connects the signals.

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

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

                    its the constructor of this widget that connects the signals.

                    well, if you do your connects in the constructor of the widget, than you shouldn't come into the situation, where the valueChanged signal is not connected, right ?


                    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 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

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

                      its the constructor of this widget that connects the signals.

                      well, if you do your connects in the constructor of the widget, than you shouldn't come into the situation, where the valueChanged signal is not connected, right ?

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

                      @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.

                      Kind Regards,
                      Sy

                      Pl45m4P 1 Reply Last reply
                      0
                      • 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 Offline
                                        JonBJ Offline
                                        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

                                          • Login

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