Results 1 to 6 of 6

Thread: manually disconnect slot

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default manually disconnect slot

    I'm trying to manually disconnect a slot from all signals (though there usually is only one).
    This seemed like a good bet:

    Qt Code:
    1. bool QObject::disconnect ( const QObject * receiver, const char * method = 0 )
    To copy to clipboard, switch view to plain text mode 

    I am assuming "method" == SLOT. So i passed the slots class instance as receiver and SLOT(myslot()) as method, but it didnt work.

    Tried this, too:

    Qt Code:
    1. bool QObject::disconnect ( const char * signal = 0, const QObject * receiver = 0, const char * method = 0 )
    To copy to clipboard, switch view to plain text mode 

    But no luck either, the slot just stays connected. Destroying and recreating the object is not a solution in this case.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: manually disconnect slot

    read my sig.


    Added after 4 minutes:


    by the way, you should read the help/manual, because that first method isn't what you want as far as I understand.

    http://qt-project.org/doc/qt-5.0/qtc...l#disconnect-5
    Last edited by amleto; 16th May 2013 at 23:54.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: manually disconnect slot

    All of the disconnect() variants require the sender to be specified either implicitly (i.e. as this) or explicitly. Connections and disconnections are made from the sending end, which also monitors the receiver to clean out connections no longer valid because of destruction of the receiver.

    Since you don't want to destroy the receiver I suggest you have it emit a signal that senders can respond to by dropping connections.

  4. #4
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: manually disconnect slot

    Since the sender is known, this works aswell (a little further down in the docs):

    QObject::disconnect(sender, SIGNAL(mysig()), this, SLOT(myslot()));

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: manually disconnect slot

    If the receiver already had a pointer to each sender that might be connected to the slot:
    Qt Code:
    1. foreach (QObject *sender, possibleSenders)
    2. sender->disconnect(this, SLOT(whatever()));
    To copy to clipboard, switch view to plain text mode 
    should work from the receiver (absent threads). I don't see what the difficulty was in this case.

    The only difficulty in this process comes when you don't know the possible senders because there's no QObject mechanism to identify them from the receiving end AFAICT.

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: manually disconnect slot

    Quote Originally Posted by ChrisW67 View Post
    If the receiver already had a pointer to each sender that might be connected to the slot:
    Qt Code:
    1. foreach (QObject *sender, possibleSenders)
    2. sender->disconnect(this, SLOT(whatever()));
    To copy to clipboard, switch view to plain text mode 
    should work from the receiver (absent threads). I don't see what the difficulty was in this case.

    The only difficulty in this process comes when you don't know the possible senders because there's no QObject mechanism to identify them from the receiving end AFAICT.
    I don't think there is either - it would break good programming practices even more than QSignalMapper already does
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. waitForReadyRead and disconnect
    By matteo.ceruti in forum Qt Programming
    Replies: 6
    Last Post: 27th November 2012, 19:28
  2. disconnect usb device
    By marco.stanzani in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2011, 19:42
  3. Disconnect slot when another is being connected
    By holst in forum Qt Programming
    Replies: 4
    Last Post: 8th September 2009, 09:49
  4. disconnect SIGNAL/SLOT directly after emitting data
    By donglebob in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2009, 22:53
  5. Manually send signal to slot
    By donmorr in forum Qt Programming
    Replies: 1
    Last Post: 29th May 2006, 15:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.