Reputation: 14671
I have a QObject, which works as a controller. This QObject has a reference to a QPushButton. This QOjbect has a method set to be fired upon QPushButton's clicked event.
Ex:
class MyController(QObject):
def __init__(self, parent=None):
super(MyController, self).__init__(parent)
self.some_ref = ....
self.button = self.some_ref.button (returns QPushButton)
self.button.clicked.connect(self.button_clicked)
# @Slot(type)
def button_clicked(self):
print 'button clicked: ', self.sender()
# print 'button clicked (no sender req.)
Here, the button_clicked won't get fired.
I tried decorating it with @Slot()
, @Slot(QObject)
, @Slot(type)
, @Slot(str)
, @Slot(int)
but still won't work.
What am I doing wrong?
If I use ..clicked.connect(lambda: self.button_clicked)
it of course works. So I assume this is a type mismatch but shouldn't @Slot(..)
decoration have fixed it?
Thank you.
Upvotes: 0
Views: 403
Reputation: 370
I don't know if the problem is that @Slot() is commented (have a # at the beginning), but this code works for me (it's in python 3, but just change the print line)
import sys
from PySide.QtGui import *
from PySide.QtCore import *
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.button = QPushButton()
self.button.setText("Test")
self.setCentralWidget(self.button)
def GetButton(self):
return self.button
class MyController(QObject):
def __init__(self, parent=None):
super(MyController, self).__init__(parent)
self.button = parent.GetButton() #(returns QPushButton)
self.button.clicked.connect(self.button_clicked)
@Slot()
def button_clicked(self):
print('button clicked: ', self.sender())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
controller = MyController(window)
window.show()
app.exec_()
sys.exit(0)
Upvotes: 1
Reputation: 1473
May be could try the released
signal instead of the clicked
signal, because the clicked signal is emitted when the button is activated (i.e. pressed down then released while the mouse cursor is inside the button).
Or you could try the #method 2 of connecting the signals.
class MyController(QObject):
def __init__(self, parent=None):
super(MyController, self).__init__(parent)
self.some_ref = ....
self.button = self.some_ref.button
# method 1
self.button.released.connect(self.button_clicked)
# method 2
self.connect(self.button, SIGNAL('released()'), self.button_clicked)
def button_clicked(self):
print "yipee it works..."
Upvotes: 0