Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Signal connected to Script, how to disconnect?



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


  • Lifetime Qt Champion

    Hi,

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



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



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



  • @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);
               }

Log in to reply