原文 http://neteue.com/article/pyqt4-signal-slot/
信号与槽的例子
可以使用QObject.connect方法来连接信号与槽
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
class MyWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.quit_button = QtGui.QPushButton("Quit", self)
layout = QtGui.QHBoxLayout()
layout.addWidget(self.quit_button)
self.setLayout(layout)
self.connect(self.quit_button, QtCore.SIGNAL("clicked()"),
QtGui.qApp, QtCore.SLOT("quit()"))
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
win = MyWidget()
win.show()
sys.exit( app.exec_() )
在这个例子中, 使用了标准的连接方式connect(发送者, 信号, 接收者, 槽), Qt中的槽可以这样理解: 是接收者中经过包装后某个方法
自定义槽(slot)
当然可以通过QtCore.pyqtSlot自义一个槽, 在旧式风格中使用是的pyqtSignature, 建议使用pyqtSlot
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
class MyWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.ok_button = QtGui.QPushButton("OK", self)
layout = QtGui.QHBoxLayout()
layout.addWidget(self.ok_button)
self.setLayout(layout)
self.connect(self.ok_button, QtCore.SIGNAL('clicked()'),
self, QtCore.SLOT("on_ok_button_clicked()"))
@QtCore.pyqtSlot()
def on_ok_button_clicked(self):
print "OK"
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
w = MyWidget()
w.show()
app.exec_()
发送信号
单击按钮后触发emit_python_list信号, 并且执行相应的槽
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
class MyWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.ok_button = QtGui.QPushButton("OK", self)
layout = QtGui.QHBoxLayout()
layout.addWidget(self.ok_button)
self.setLayout(layout)
self.connect(self.ok_button, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("slot1()"))
self.connect(self, QtCore.SIGNAL('emit_python_list(PyQt_PyObject)'),
self, QtCore.SLOT("slot2(PyQt_PyObject)"))
@QtCore.pyqtSlot()
def on_button_clicked(self):
print "click me!!!!"
@QtCore.pyqtSlot()
def slot1(self):
self.emit(QtCore.SIGNAL("emit_python_list(PyQt_PyObject)"), [1, 2, 3, 4, 5, 6])
@QtCore.pyqtSlot("PyQt_PyObject")
def slot2(self, alist):
print alist
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
w = MyWidget()
w.show()
app.exec_()
执行Python方法
这里使用更加优雅直接的方式连接信号并执行回调, 这也是项目中用到最多的方式
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
class MyWidget(QtGui.QWidget):
def __init__(sel`f, parent=None):
super(MyWidget, self).__init__(parent)
self.ok_button = QtGui.QPushButton("ok", self)
layout = QtGui.QHBoxLayout()
layout.addWidget(self.ok_button)
self.setLayout(layout)
self.ok_button.clicked.connect(self.on_ok_button_clicked)
def on_ok_button_clicked(self):
print "OK"
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
win = MyWidget()
win.show()
sys.exit( app.exec_() )
self.ok_button.clicked.connect 其中clicked是信号
注册信号
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
class MyWidget(QtGui.QWidget):
emit_python_list = QtCore.pyqtSignal(object)
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.ok_button = QtGui.QPushButton("OK", self)
layout = QtGui.QHBoxLayout()
layout.addWidget(self.ok_button)
self.setLayout(layout)
self.ok_button.clicked.connect(self.slot1)
self.emit_python_list.connect(self.slot2)
def slot1(self):
self.emit_python_list.emit([1, 2, 3, 4, 5, 6])
def slot2(self, alist):
print alist
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
w = MyWidget()
w.show()
app.exec_()
自动连接
QMetaObject. connectSlotsByName(QObject)
其作用是如其名称一样,用来将QObject 里的子孙QObject的某些信号按照其objectName连接到相应的槽上,
如是使用pyuic生成的代码, 就是使用这种方式
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
class MyWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.ok_button = QtGui.QPushButton("OK", self)
self.ok_button.setObjectName("ok_button")
layout = QtGui.QHBoxLayout()
layout.addWidget(self.ok_button)
self.setLayout(layout)
QtCore.QMetaObject.connectSlotsByName(self)
@QtCore.pyqtSlot() # 一定要有,不然会打印两遍 "OK"
def on_ok_button_clicked(self):
print "OK"
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
w = MyWidget()
w.show()
app.exec_()
别忘了, 使用setObjectName设置对象名称
border="0" width="330" height="86" src="http://music.163.com/outchain/player?type=2&id=28302615&auto=1&height=66">