[PyKDE] slots and callables.

Toby Sargeant t.sargeant at inpharmatica.co.uk
Wed May 30 10:46:48 BST 2001


does anyone know why it seems that although you can use functions and bound
methods as slots, you can't use other types of callable?

import qt

class Callable:
  def __init__(self,callable):
    self.callable=callable
  def __call__(self,*args,**kw):
    self.callable(*args,**kw)

def f():
  print "foo"

app=qt.QApplication([])
box=qt.QLineEdit(None)
box.show()

box.connect(box,qt.SIGNAL('textChanged(const QString &)'),Callable(f))
box.connect(box,qt.SIGNAL('textChanged(const QString &)'),Callable(lambda:f))
box.connect(box,qt.SIGNAL('textChanged(const QString &)'),lambda:f)
box.connect(box,qt.SIGNAL('textChanged(const QString &)'),f)

app.setMainWidget(box)
app.exec_loop()

-- of the four connects, only the last works, even though the three prior to
it are functionally equivalent. being able to use lambdas at least would be
very handy. As an example, say you wanted to synchronize the position of
a group of tables. With lambdas, you could write:

class TableManager(qt.QWidget):
  def tableMoving(self,x,y,t):
     for tablenum in range(len(self.tables)):
        if tablenum==t:
           continue
        ...

  def __init__(self,parent=None,name=None):
     ...
     for tablenum in range(len(self.tables)):
        self.connect(self.tables[tablenum],
                     qt.SIGNAL('contentsMoving(int,int)'),
                     lambda x,y,self=self,t=tablenum:self.tableMoving(x,y,t))

without them, I can't see how tableMoving can know which table is being
moved by the user, and it seems that even though you can sort out the
infinite recursion that can result from moving _all_ of the tables to the
new location, the result seems to be that the update doesn't occur properly.

Any thoughts/clues?

Toby.

-- 
   :bump: vt.  Synonym for increment.  Has the same meaning as C's
++ operator.  Used esp. of counter variables, pointers, and index dummies
in `for', `while', and `do-while' loops.

  [ Toby Sargeant : Inpharmatica : Developer : t.sargeant at inpharmatica.co.uk ]
      [ http://www.inpharmatica.co.uk : 020 7631 4644 fax 020 7631 4844 ]




More information about the PyQt mailing list