PDA

View Full Version : manually disconnect slot



tuli
16th May 2013, 22:59
I'm trying to manually disconnect a slot from all signals (though there usually is only one).
This seemed like a good bet:


bool QObject::disconnect ( const QObject * receiver, const char * method = 0 )

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:


bool QObject::disconnect ( const char * signal = 0, const QObject * receiver = 0, const char * method = 0 )

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

amleto
16th May 2013, 23:54
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/qtcore/qobject.html#disconnect-5

ChrisW67
17th May 2013, 01:50
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.

tuli
17th May 2013, 06:03
Since the sender is known, this works aswell (a little further down in the docs):


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

ChrisW67
17th May 2013, 08:41
If the receiver already had a pointer to each sender that might be connected to the slot:


foreach (QObject *sender, possibleSenders)
sender->disconnect(this, SLOT(whatever()));

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.

amleto
17th May 2013, 13:45
If the receiver already had a pointer to each sender that might be connected to the slot:


foreach (QObject *sender, possibleSenders)
sender->disconnect(this, SLOT(whatever()));

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