I am using a partial function to pass a value to a function argument, on a click of a QPushButton.
self.SearchButton1 = QPushButton("Search")
self.SearchStudent1 = QLineEdit()
Below is how it is connected:
self.connect(self.SearchButton1, SIGNAL("clicked()"),
partial(self.Searching, self.SearchStudent1.text()))
The called function is as below:
def Searching(self, num):
print self.SearchStudent1.text()
print num
I am not sure why num
is not getting printed, whereas self.SearchStudent1.text()
gets printed correctly on the press of the 'Search' button.
Please suggest if I am missing anything.
EDIT :
I can anyhow pass the QLineEdit object using partial function and work with it:
self.connect(self.SearchButton1, SIGNAL("clicked()"),
partial(self.Searching, self.SearchStudent1))
def Searching(self, num):
print self.SearchStudent1.text()
print num.text() # works fine
The partial function will cache the arguments passed to it. So if the line-edit is empty when the signal is connected, an empty string will always be sent to the called method. (It's always a good idea to use repr
when debugging with print
, so that you can easily see output values like ""
or None
).
If you want the current text is be sent to the slot when the button is clicked, you can use a lambda
function, like this:
self.connect(self.SearchButton1, SIGNAL("clicked()"),
lambda: self.Searching(self.SearchStudent1.text())
Or more simply:
self.SearchButton1.clicked.connect(
lambda: self.Searching(self.SearchStudent1.text())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.