(2018/08/25) We received a comment that this event has been fixed in Qt for Python 5.11.2. Therefore, the following contents are out of date.
Personal notes. Maybe I'll write in a little more detail later.
PySide (or perhaps Qt) has the concepts of "signals" and "slots" as a mechanism for handling GUI events.
In the following, the button_clicked method defined by myself is linked as a slot to the signal'clicked'.
ex1
class Hoge(QWidget):
def __init__(self):
super(Hoge, self).__init__()
self.button1 = QPushButton('Button1', self)
self.button1.clicked.connect(self.button_clicked)
def button_clicked(self):
sender = self.sender()
self.window().statusBar().showMessage(sender.text() + ' was clicked')
'clicked' is literally associated with a click event. When the corresponding object (button in the above example) is clicked, the associated slot (button_clicked) is called.
So, here is a case where I got a little stuck today. You can specify the event source object by calling self.sender () in the method specified as the slot. At that time, if the method specified as the slot is named mangling, self.sender () returns None, and the source object could not be acquired successfully.
ex2
class Hoge(QWidget):
def __init__(self):
super(Hoge, self).__init__()
self.button1 = QPushButton('Button1', self)
self.button1.clicked.connect(self.button_clicked)
self.button2 = QPushButton('Button1', self)
self.button2.clicked.connect(self.__button_clicked)
#Do not name mangling
def button_clicked(self):
sender = self.sender()
print sender.text()
#Name mangling
def __button_clicked(self):
sender = self.sender()
print sender.text()
The following is the output at that time.
Button1 (Button1 can get the origin)
Traceback (most recent call last): (Button2 can't work)
File "sandbox/myevent.py", line 77, in __button_clicked
print sender.text()
AttributeError: 'NoneType' object has no attribute 'text'
The cause has not been caught up yet, but if you specify it as a slot for the time being, do not name mangling ...
Recommended Posts