Hi,

I encounter a problem using "emit" method of a signal as a slot of
another signal. Problem occurs using "disconnect".

Here is sample code, 'ok' and 'cancel' inherit from QAbstractButton:

ok.pressed.connect(cancel.pressed.emit)
ok.pressed.disconnect(cancel.pressed.emit)

The second statement raise an exception:
"TypeError: 'builtin_function_or_method' object is not connected"

Calling "disconnect" without parameter works fine.

If I use an intermediate function, TypeError is not raised:

def reemit():
    cancel.pressed.emit()

ok.pressed.connect(reemit)
ok.pressed.disconnect(reemit)

Behaviour is the same with old style signals.

Is there a way to not use an intermediate function ?

sip: 4.12.4
PyQt: 4.8.3
Qt: 4.7.3

Test cases in attachment.

Thanks,

Pierre-Louis
#!/usr/bin/python
from sys import argv
from PyQt4.QtGui import QDialog, QDialogButtonBox, QApplication
from PyQt4.QtCore import pyqtSlot, SIGNAL

@pyqtSlot()
def cancel_callback():
    print "cancel_callback"

def create_widgets():
    dialog = QDialog()
    box = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel, parent=dialog)

    ok = box.button(QDialogButtonBox.Ok)
    cancel = box.button(QDialogButtonBox.Cancel)

    cancel.pressed.connect(cancel_callback)
    return dialog, ok, cancel

def test1(dialog, ok, cancel):
    """new style signal using disconnect without parameter"""
    ok.pressed.connect(cancel.pressed.emit)
    ok.pressed.disconnect()
    # ok no problem here

def test2(dialog, ok, cancel):
    """new style signal using disconnect with emit"""
    ok.pressed.connect(cancel.pressed.emit)
    ok.pressed.disconnect(cancel.pressed.emit)
    # raise "TypeError: 'builtin_function_or_method' object is not connected"

def test3(dialog, ok, cancel):
    """new style signal using disconnect with an intermediate function"""
    def reemit():
        cancel.pressed.emit()

    ok.pressed.connect(reemit)
    ok.pressed.disconnect(reemit)
    # ok

def test4(dialog, ok, cancel):
    """old style signal using disconnect with emit"""
    ret = ok.connect(ok, SIGNAL('pressed()'), cancel.pressed.emit) # -> return True
    assert ret, "connect failed"
    ret = ok.disconnect(ok, SIGNAL('pressed()'), cancel.pressed.emit) # -> return False
    assert ret, "disconnect failed"

def test5(dialog, ok, cancel):
    """old style signal using disconnect with an intermediate function"""
    def reemit():
        cancel.pressed.emit()

    ret = ok.connect(ok, SIGNAL('pressed()'), reemit) # -> return True
    assert ret, "connect failed"
    ret = ok.disconnect(ok, SIGNAL('pressed()'), reemit) # -> return True
    assert ret, "disconnect failed"

if __name__ == "__main__":
    app = QApplication(argv)

    for test in test1, test2, test3, test4, test5:
        print test.__doc__
        try:
            dialog, ok, cancel = create_widgets()
            test(dialog, ok, cancel)
        except Exception, err:
            print "'ok' button: signal not disconnected"
            print "'%s' failed: '%s'" % (test, err)
        else:
            print "'ok' button: signal disconnected"
            print "'%s' successful" % test
        dialog.exec_()
        print ""
_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to