[PyQt] Strangeness with signals and slots in PyQt4 QLineEdit

Johannes Lochmann (gmail) johannes.lochmann at googlemail.com
Thu Aug 16 14:57:37 BST 2007


Hello all!

I have written lots of code in Qt3/C++ and am evaluating PyQt4 for a new 
project right now. However, I encountered a strange behaviour, that I can not 
explain (code at the bottom of this post): It seems to me, that the signals 
and slots are acting rather unpedictable:

I have a simple widget with three QLineEdits and connect the signal 
textEdited(QString) to my slots. In the slots I set the new text of the 
lineedit to a data class (and print them).

If I enter 'a', 'b' and 'c' in the fields labelled 'a', 'b', 'c', I would have 
expected to see something like this:

a:None:None
a:b:None
a:b:c

But I get:

a:None:None
b:b:None
c:c:c

Could anybody be so kind and hit me with a cluebat?

Thank you!

Here is the code:

#!/usr/bin/python

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

""" A Simple test data class """
class Data:
	def __init__(self, a=None, b=None, c=None):
		self.a = a
		self.b = b
		self.c = c
	def __str__(self):
		return "%s:%s:%s" % (self.a, self.b, self.c)
		
""" A Test widget """
class Widget(QWidget):
	def __init__(self):
		QWidget.__init__(self, None)
		self.data = Data()
		
		aw = QLineEdit(self)
		bw = QLineEdit(self)
		cw = QLineEdit(self)
		
		l = QGridLayout(self)
		l.addWidget(QLabel("a", self), 0, 0)
		l.addWidget(aw, 0, 1)
		l.addWidget(QLabel("b", self), 1, 0)
		l.addWidget(bw, 1, 1)
		l.addWidget(QLabel("c", self), 2, 0)
		l.addWidget(cw, 2, 1)
		self.setLayout(l)
		
		self.connect(aw, SIGNAL("textEdited(QString)"), self.onAChange)
		self.connect(bw, SIGNAL("textEdited(QString)"), self.onBChange)
		self.connect(cw, SIGNAL("textEdited(QString)"), self.onCChange)
		
	def onAChange(self, txt):
		self.data.a = txt
		print "onAChange: %s" % self.data
	def onBChange(self, txt):
		self.data.b = txt
		print "onBChange: %s" % self.data
	def onCChange(self, txt):
		self.data.c = txt
		print "onCChange: %s" % self.data
		
if "__main__" == __name__:
	app = QApplication(sys.argv)
	w = Widget()
	w.show()
	sys.exit(app.exec_())


More information about the PyQt mailing list