0

I’m having a reoccurring problem when trying to connect a method to a signal in Pyside

Inside my class :

myButton = QPushButton()
someLineEdit = QLineEdit()
myButton.clicked.connect(self._someMethod (someLineEdit))

The _someMethod function is a class method

When I include arguments in my method, the error states ā€˜ Failed to connect signal clicked()’

If I remove arguments from the method, it connects fine. I have tested this passing in a simple string instead, but still get the same error

Is there a workaround , or known limitations in using connect in this manner ?

MrLeeh
  • 5,321
  • 6
  • 33
  • 51
blam
  • 87
  • 1
  • 8

2 Answers2

0

You need to pass the callable function (or slot) as an argument to your connect() function for this to work. The way you do it now you call the function self.someMethod() and pass the result to connect(). Change your code like this:

myButton.clicked.connect(self._someMethod)

def self._someMethod():
    # do something here
    print(self.sender())  # this prints the sender of the event        
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
MrLeeh
  • 5,321
  • 6
  • 33
  • 51
0

You can use partial for that:

from functools import partial

myButton.clicked.connect(partial(self._someMethod, someLineEdit))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
noEmbryo
  • 2,176
  • 2
  • 10
  • 15