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. Signal connected to Script, how to disconnect?
Forum Updated to NodeBB v4.3 + New Features

Signal connected to Script, how to disconnect?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 537 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 signal which is connected to a script, this works great. The question I have is, how do I disconnect the signal from the script?

    At present when I click on a button it connects the script again, but result is the script is actioned as many times as the connection is made.

    What I ideally want to do is disconnect the signal and script before reconnecting.

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Might be a silly question but why not connect only once ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

        @SGaist, yes, that's an option, but I also want to be able to disconnect as an option.

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

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

          Is shows disconnect as a call for QObject. I think we use that in our own codebase too.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          2
          • SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by SPlatten
            #5

            @fcarney , thank you, actually I think I've found a solution, its much easier than I first thought. QObject::connect returns a QMetaObject::Connection, this can be tested for a connection before reconnecting.

            [Edit] I modified my code, adding a member variable to the class 'QMetaObject::Connection mSignalConn' then in the function before I connect:

                        if ( mSignalConn != nullptr ) {
                            QObject::disconnect(mSignalConn);
                       }
            

            Kind Regards,
            Sy

            1 Reply Last reply
            1

            • Login

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