[PYTHON] Notes on defining PySide slots

Caution

(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

Notes on defining PySide slots (2)
Notes on defining PySide slots
Notes on Flask
Notes on neural networks
Celery notes on Django
Notes on installing PycURL
Notes on using Alembic
Notes on SciPy.linalg functions
Notes on tf.function and Tracing
Notes on installing dlib on mac
Notes on python's sqlite3 module
Notes on * args and ** kargs
[Django] Notes on using django-debug-toolbar
Notes on pyenv and Atom
[Python] Notes on data analysis
Notes on optimization using Pytorch
Notes on installing Python on Mac
Notes on studying multidimensional scaling
Notes on installing pipenv on Mac
Notes on installing Anaconda 3 on Windows
Notes on imshow () in OpenCV
Notes on installing Python on CentOS
Notes on Python and dictionary types
Notes on package management with conda
Notes on using MeCab from Python
[Golang] Notes on frequently used functions
Notes on how to use pywinauto
Notes on using post-receive and post-merge
Notes on how to use featuretools
Notes on installing Python using PyEnv
Notes on using rstrip with python.
Notes on accessing dashDB from python
Notes on how to use doctest
Notes on using matplotlib on the server
Notes on how to write requirements.txt
Notes on installing Ubuntu 18.04 on the XPS 15 7590
(Beginner) Notes on using pyenv on Mac