<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
    <title></title>
  </head>
  <body text="#000000" bgcolor="#ffffff">
    Le 28/08/11 15:11, bluekyu a écrit :
    <blockquote cite="mid:4E5A3E66.4040605@gmail.com" type="cite">Hello.
      I have a question about "partial" and "lambda" in connect slot.
      <br>
      <br>
      I learned that "partial" and "lambda" can have arguments in
      connect slot.
      <br>
      So, I usually use lambda function, however, it does not work well
      in some code.
      <br>
      <br>
      My example code:
      <br>
      --------------------------------------------
      <br>
      from PyQt4.QtCore import *
      <br>
      from PyQt4.QtGui import *
      <br>
      import sys
      <br>
      from functools import partial
      <br>
      <br>
      app = QApplication(sys.argv)
      <br>
      form = QDialog()
      <br>
      layout = QVBoxLayout()
      <br>
      <br>
      label = QLabel("Test")
      <br>
      layout.addWidget(label)
      <br>
      <br>
      stringList = ["ABC", "123", "XYZ", "789"]
      <br>
      for string in stringList:
      <br>
          button = QPushButton(string)
      <br>
          layout.addWidget(button)
      <br>
          form.connect(button, SIGNAL("clicked()"),
      <br>
      #                    lambda: label.setText(string))    # Problem
      <br>
                          partial(label.setText, string))
      <br>
      <br>
      form.setLayout(layout)
      <br>
      form.show()
      <br>
      app.exec_()
      <br>
      ---------------------------------------------
      <br>
      <br>
      I expected that each button set label text as each string, but all
      button set label text as "789".
      <br>
      Instead, partial function works very well.
      <br>
      <br>
      What is difference between "partial" and "lambda"?
      <br>
      _______________________________________________
      <br>
      PyQt mailing list    <a class="moz-txt-link-abbreviated" href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a>
      <br>
      <a class="moz-txt-link-freetext" href="http://www.riverbankcomputing.com/mailman/listinfo/pyqt">http://www.riverbankcomputing.com/mailman/listinfo/pyqt</a>
      <br>
      <br>
    </blockquote>
    Your's lambda function is not correct.<br>
    <br>
    Use this:<br>
    <br>
        form.connect(button, SIGNAL("clicked()"),<br>
                        lambda s=string: label.setText(s))<br>
    <br>
    <br>
    <br>
    <div class="moz-signature">-- <br>
      Vincent V.V.<br>
      <a href="https://launchpad.net/oqapy">Oqapy</a> . <a
        href="https://launchpad.net/qarte+7">Qarte+7</a> . <a
        href="https://launchpad.net/paqager">PaQager</a></div>
  </body>
</html>