Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. button.clicked.connect(), pass a nested function as parameter
QtWS25 Last Chance

button.clicked.connect(), pass a nested function as parameter

Scheduled Pinned Locked Moved Solved Language Bindings
3 Posts 2 Posters 11.5k 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.
  • E Offline
    E Offline
    EscapistGorilla
    wrote on last edited by
    #1

    Hi!

    I'm working on some code which contains a QPushButton and a QLineEdit.
    When the QPushButton is pressed, one function will get some text from an external program, and then another function will transcribe this file path into the QLineEdit widget. However, I'm having some trouble passing these nested functions as the 'button.clicked.connect()' parameter:

    Code snippet:

    ...
    class Tool(QtWidgets.QMainWindow):
        def __init__(self, win_id=None, parent=None):
            ...
            # the text box
            self.textBox = QtWidgets.QLineEdit()
    
            # the button
            self.button= QtWidgets.QPushButton()
            self.button.clicked.connect() # insert code here!
            ...
    
        def getExtText(self):
            """Get some text from an external program"""
            return ExtProgram.text
    
        def updateTextBox(self, str):
            """Update the editable text box."
            self.TXT_FileSelectedPath.setPlaceholderText(str)
    ...
    

    I'd really prefer to keep these two functions separate because there are a bunch of other, similar use cases.
    Regarding the button's click event, here's what I've tried. I'm unfamiliar with 'lambda' and 'partial', but I've gone through some tutorials and I think my syntax is correct.

    self.button.clicked.connect(self.updateTextBox(self.getExtText))
    
    self.button.clicked.connect(lambda: self.updateTextBox(self.getExtText))
    
    self.button.clicked.connect(partial(self.updateTextBox, self.getExtText))
    

    If you have any advice, I'd really appreciate it. Thank you!

    JonBJ 1 Reply Last reply
    0
    • E EscapistGorilla

      Hi!

      I'm working on some code which contains a QPushButton and a QLineEdit.
      When the QPushButton is pressed, one function will get some text from an external program, and then another function will transcribe this file path into the QLineEdit widget. However, I'm having some trouble passing these nested functions as the 'button.clicked.connect()' parameter:

      Code snippet:

      ...
      class Tool(QtWidgets.QMainWindow):
          def __init__(self, win_id=None, parent=None):
              ...
              # the text box
              self.textBox = QtWidgets.QLineEdit()
      
              # the button
              self.button= QtWidgets.QPushButton()
              self.button.clicked.connect() # insert code here!
              ...
      
          def getExtText(self):
              """Get some text from an external program"""
              return ExtProgram.text
      
          def updateTextBox(self, str):
              """Update the editable text box."
              self.TXT_FileSelectedPath.setPlaceholderText(str)
      ...
      

      I'd really prefer to keep these two functions separate because there are a bunch of other, similar use cases.
      Regarding the button's click event, here's what I've tried. I'm unfamiliar with 'lambda' and 'partial', but I've gone through some tutorials and I think my syntax is correct.

      self.button.clicked.connect(self.updateTextBox(self.getExtText))
      
      self.button.clicked.connect(lambda: self.updateTextBox(self.getExtText))
      
      self.button.clicked.connect(partial(self.updateTextBox, self.getExtText))
      

      If you have any advice, I'd really appreciate it. Thank you!

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

      @EscapistGorilla
      First one won't work. I don't know about Python partials so can't comment on third one.

      Second one is usual approach with lambda for passing your own parameter to a slot. But self.getExtText is a reference to a function/callable, it does not actually call self.getExtText. Start with

      self.button.clicked.connect(lambda: self.updateTextBox(self.getExtText()))
      

      and come back if that does not do what you intended.

      1 Reply Last reply
      1
      • E Offline
        E Offline
        EscapistGorilla
        wrote on last edited by
        #3

        This worked, thank you!

        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