I'll just give you a short intro to whet your appetite, find all details here yourself.
import sys from PyQt4 import QtCore from PyQt4 import QtGui def clicked(): print "Button Clicked" qApp = QtGui.QApplication(sys.argv) button = QtGui.QPushButton("Click Me") QtCore.QObject.connect(button, QtCore.SIGNAL('clicked()'), clicked) button.show() sys.exit(qApp.exec_())This is the old way of connecting a signal to a slot. To use the new-style support just replace line 11 with following code
button.clicked.connect(clicked)The new-style support introduces an attribute with the same name as the signal, in this case clicked.
If you need to define your own signal you'll do something like this (off the top of my head):
class X(QtCore.QObject): mySignal = QtCore.pyqtSignal(int) def emitMySignal(self): self.mySignal.emit(100)And the old way:
class X(QtCore.QObject): def emitMySignal(self): self.emit(QtCore.SIGNAL('mySignal'), 100)
IMHO the new-style support is more pythonic and you don't have to specify your signals as strings when connecting. If you use pydev (eclipse) you'll also have completion support for signals.
An interesting variation you can use when defining Python slots on the fly
ReplyDeleteis to use the signal as a decorator:
@button.clicked.connect
def clicked():
print "Button clicked"
gldnspud: Ah, sweet. Have to try it.
ReplyDeletei think the correct function is
ReplyDeletemySignal = QtCore.pyqtSignal(int)
Anonymous you are right, there is mistake. Author of blog should correct it.
ReplyDelete