Le 28/08/11 15:11, bluekyu a écrit :
Hello. I have a question about "partial" and "lambda" in connect slot.

I learned that "partial" and "lambda" can have arguments in connect slot.
So, I usually use lambda function, however, it does not work well in some code.

My example code:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from functools import partial

app = QApplication(sys.argv)
form = QDialog()
layout = QVBoxLayout()

label = QLabel("Test")

stringList = ["ABC", "123", "XYZ", "789"]
for string in stringList:
    button = QPushButton(string)
    form.connect(button, SIGNAL("clicked()"),
#                    lambda: label.setText(string))    # Problem
                    partial(label.setText, string))


I expected that each button set label text as each string, but all button set label text as "789".
Instead, partial function works very well.

What is difference between "partial" and "lambda"?
PyQt mailing list    PyQt@riverbankcomputing.com

Your's lambda function is not correct.

Use this:

    form.connect(button, SIGNAL("clicked()"),
                    lambda s=string: label.setText(s))

Vincent V.V.
Oqapy . Qarte+7 . PaQager
PyQt mailing list    PyQt@riverbankcomputing.com

Reply via email to