Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. [Pyside2] Multiple QLineEdit signals not working
Forum Updated to NodeBB v4.3 + New Features

[Pyside2] Multiple QLineEdit signals not working

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 650 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Geoffrey
    wrote on last edited by
    #1

    Hi !
    Sorry in advance for my english, it's not my main language.

    I try to have this code : objQLineEdit.signal.connect(self.function) working under a for loop.
    For testing, i just use a simple print.

    For example with this :

    for i in range(10):
    objQLineEdit = QLineEdit()
    objQLineEdit.signal.connect(lambda: self.function(objQLineEdit.text()))
    <END CODE>

    Only the last QLineEdit has a signal that is working.
    How can i get all of them work ?

    Thank's in advance.

    JonBJ 1 Reply Last reply
    0
    • G Geoffrey

      Hi !
      Sorry in advance for my english, it's not my main language.

      I try to have this code : objQLineEdit.signal.connect(self.function) working under a for loop.
      For testing, i just use a simple print.

      For example with this :

      for i in range(10):
      objQLineEdit = QLineEdit()
      objQLineEdit.signal.connect(lambda: self.function(objQLineEdit.text()))
      <END CODE>

      Only the last QLineEdit has a signal that is working.
      How can i get all of them work ?

      Thank's in advance.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @Geoffrey said in [Pyside2] Multiple QLineEdit signals not working:

      Only the last QLineEdit has a signal that is working.

      I do not believe that is so. Replace by:

      objQLineEdit.signal.connect(lambda: print("Some signal"))
      

      (I don't know what your particular objQLineEdit.signal is, I presume you have something like clicked there, not actually signal. I assume your whole code is an "outline", not real code.)

      Does that indeed print when any of the edits are pressed? I believe it should.

      What you may have a problem with in Python is your particular lambda:

      objQLineEdit.signal.connect(lambda: self.function(objQLineEdit.text()))
      

      I'm not 100% about this, but I think that will only ever pass the last value of objQLineEdit. Try:

      objQLineEdit.signal.connect(lambda lineEdit=objQLineEdit: self.function(lineEdit.text()))
      

      If that works, this is a Python-esque issue.

      G 1 Reply Last reply
      1
      • JonBJ JonB

        @Geoffrey said in [Pyside2] Multiple QLineEdit signals not working:

        Only the last QLineEdit has a signal that is working.

        I do not believe that is so. Replace by:

        objQLineEdit.signal.connect(lambda: print("Some signal"))
        

        (I don't know what your particular objQLineEdit.signal is, I presume you have something like clicked there, not actually signal. I assume your whole code is an "outline", not real code.)

        Does that indeed print when any of the edits are pressed? I believe it should.

        What you may have a problem with in Python is your particular lambda:

        objQLineEdit.signal.connect(lambda: self.function(objQLineEdit.text()))
        

        I'm not 100% about this, but I think that will only ever pass the last value of objQLineEdit. Try:

        objQLineEdit.signal.connect(lambda lineEdit=objQLineEdit: self.function(lineEdit.text()))
        

        If that works, this is a Python-esque issue.

        G Offline
        G Offline
        Geoffrey
        wrote on last edited by
        #3

        @JonB

        You are right ! It was indeed the lambda the real problem.
        And yes it is an "outline" code.

        Thank you its working ! :)

        JonBJ 1 Reply Last reply
        0
        • G Geoffrey

          @JonB

          You are right ! It was indeed the lambda the real problem.
          And yes it is an "outline" code.

          Thank you its working ! :)

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @Geoffrey
          This business of needing in Python to write:

          for objQLineEdit in ...:
              objQLineEdit.signal.connect(lambda lineEdit=objQLineEdit: self.function(lineEdit.text()))
          

          really threw me when I first came across it. It works differently from the C++ lambda:

          connect(objQLineEdit,  &Class::signal, [objQLineEdit]() { qDebug() << objLineEdit.text(); });
          

          In the C++ you get the lambda's objLineEdit to be the current value of objQLineEdit when it was set up, like you'd expect.

          But in the Python

          objQLineEdit.signal.connect(lambda: self.function(objQLineEdit.text()))
          

          you get all the lambdas connecting to the last value objQLineEdit had when the loop was run. Passing it as a parameter to the lambda by

          objQLineEdit.signal.connect(lambda lineEdit=objQLineEdit: self.function(lineEdit.text()))
          

          seems to be the equivalent of the C++ lambda, so that it references the objQLineEdit as it was when the line was executed.

          Keep this in mind, as it comes up again whenever you write Python lambdas in Qt! :)

          G 1 Reply Last reply
          1
          • JonBJ JonB

            @Geoffrey
            This business of needing in Python to write:

            for objQLineEdit in ...:
                objQLineEdit.signal.connect(lambda lineEdit=objQLineEdit: self.function(lineEdit.text()))
            

            really threw me when I first came across it. It works differently from the C++ lambda:

            connect(objQLineEdit,  &Class::signal, [objQLineEdit]() { qDebug() << objLineEdit.text(); });
            

            In the C++ you get the lambda's objLineEdit to be the current value of objQLineEdit when it was set up, like you'd expect.

            But in the Python

            objQLineEdit.signal.connect(lambda: self.function(objQLineEdit.text()))
            

            you get all the lambdas connecting to the last value objQLineEdit had when the loop was run. Passing it as a parameter to the lambda by

            objQLineEdit.signal.connect(lambda lineEdit=objQLineEdit: self.function(lineEdit.text()))
            

            seems to be the equivalent of the C++ lambda, so that it references the objQLineEdit as it was when the line was executed.

            Keep this in mind, as it comes up again whenever you write Python lambdas in Qt! :)

            G Offline
            G Offline
            Geoffrey
            wrote on last edited by
            #5

            @JonB said in [Pyside2] Multiple QLineEdit signals not working:

            @Geoffrey
            This business of needing in Python to write:

            for objQLineEdit in ...:
                objQLineEdit.signal.connect(lambda lineEdit=objQLineEdit: self.function(lineEdit.text()))
            

            really threw me when I first came across it.

            I have the same reaction :p

            Keep this in mind, as it comes up again whenever you write Python lambdas in Qt! :)

            Yes i try to not reproduce twice the same mistake and learn from it :)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved