I came across this weirdness in using PySide Slot decorator. If I decorate my method using QtCore.Slot and if I try to access self.sender() inside the method, I get None. If I remove the QtCore.Slot() decorator. I get the sender properly. Here is a minimal example.
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class Worker(QObject):
def init(self):
print "worker is ready."
@Slot()
def work(self):
print "i am tired, %s" % self.sender()
app = QApplication(sys.argv)
button = QPushButton("Kick!")
button.show()
worker = Worker()
thread = QThread()
worker.moveToThread(thread)
thread.started.connect(worker.init)
button.clicked.connect(worker.work)
# app.connect(button, SIGNAL("clicked()"), worker, SLOT("work()"))
thread.start()
app.exec_()
sys.exit()
However, if I change the new style connection to the old way, as shown in the commented line.
It works. Can someone explain this behavior? Thanks a lot.
The problem is that the object that receive the signal (your Worker class) lives in another thread.
From the Qt docs:
If you don't move the object to the other thread, it works (the examples is in python3, but it will work in python 2, after changing the print lines):