[PyKDE] Emits and connects
Doug Bell
dougbell at mnsinc.com
Wed Jul 4 17:16:25 BST 2001
Jan Ekholm wrote:
>
> Hi all,
>
> I've used PyQt for a while with great success. Makes it really easy to do
> apps. Now I did run into a problem with signals and slots. I want to emit
> a custom signal in a class along with a few parameters and connect it to a
> Python method. I've tried the following (rough pseudocode):
>
> class A:
>
> def foo():
> self.emit (PYSIGNAL('oops(int,int)'), (x,y))
> self.emit (PYSIGNAL('oops'), (x, y))
>
>
> class B:
>
> def bar():
> a = A ()
> self.connect ( a, PYSIGNAL('oops(int,int)'), self.oops )
> self.connect ( a, PYSIGNAL('oops'), self.oops )
>
> def oops (self,x,y):
> pass
>
>
> None of the code gives any error, the emit():s are done ok, the
> connect():s too (I usually get runtime errors if something is wrong), but
> the callback oops() is just never called. There is something simple wrong
> here, but what could that be?
>
> Any hints? I use Linux, PyQt 2.4-pre1, Qt 2.3.0, sip-2.4pre2-1.
The second version of the emit and the second version of the connect
are what I usually use, but both versions do work. There must be
something else wrong.
It's hard to tell from the pseudocode, though. Of course, both
classes need to be derived from QObject, and the instance 'a' is a
temporary object. But those are because of the pseudocode, I'm sure.
Try this actual test code (it works for me):
#!/usr/bin/env python
from qt import *
class A(QObject):
def __init__(self):
QObject.__init__(self)
def foo(self):
print 'emitting'
self.emit(PYSIGNAL('oops'), (3, 4))
class B(QObject):
def __init__(self):
QObject.__init__(self)
self.a = A()
self.connect(self.a, PYSIGNAL('oops'), self.oops)
def oops(self, x, y):
print 'here'
b = B()
b.a.foo()
-----------------------------------------
Doug.
More information about the PyQt
mailing list