Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved QPushButton clicked connect strange behavior

    Qt for Python
    2
    3
    32
    Loading More Posts
    • 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.
    • F
      ForeverNoob last edited by

      Hello,

      Got the following code:

      class NoteManagerClass(QMainWindow):
      
          def __init__(self):
              super().__init__()
      
              # Widgets
              newNoteButton = QPushButton("New Note")
              newNoteButton.clicked.connect(self.newNote())        # This actually calls the newNote() function
              .
              . more lines
              .
      
          def newNote(self):
              print("New Note")
      
      

      The problem: when the script reaches the button connect function, it executes the newNote function, but later, when the button is clicked, that function isn't called. Not sure whats going on.

      (Sorry in advance for possibly stupid question, newbie in both Qt and python)

      TIA

      Chris Kawa 1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators @ForeverNoob last edited by

        This actually calls the newNote() function

        Yes, because you called it (used the () operator). You're supposed to pass the function address, not call it i.e.

        newNoteButton.clicked.connect(self.newNote)  
        
        F 1 Reply Last reply Reply Quote 2
        • Topic has been marked as solved  F ForeverNoob 
        • F
          ForeverNoob @Chris Kawa last edited by

          @Chris-Kawa said in QPushButton clicked connect strange behavior:

          This actually calls the newNote() function

          Yes, because you called it (used the () operator). You're supposed to pass the function address, not call it i.e.

          newNoteButton.clicked.connect(self.newNote)  
          

          Right, it works now. Thanks a bunch.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post