#coding=utf-8
__author__ = 'Administrator'
__doc__ = \
'''
python版本的QT中信号槽的使用方法
'''
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
class MainWindow(QMainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
#初始化Actions
self._cut = QAction(u'剪切', self)
self._copy = QAction(u'复制', self)
self._paste = QAction(u'粘贴', self)
#初始化工具栏
self._toolBar = QToolBar()
self.addToolBar(self._toolBar)
actions = [self._cut, self._copy, self._paste,]
for a in actions:
#第一种信号槽的连接方式
#self.connect(a, SIGNAL('triggered()'), SLOT('onTrigger()'))
#第二种
a.triggered.connect(self.onTrigger2)
#第三种信号槽的连接方式
self.connect(a, SIGNAL('triggered()'), self,SLOT('onTrigger()'))
self._toolBar.addAction(a)
self.statusBar = QStatusBar()
self.setStatusBar(self.statusBar)
def doTrigger(self):
act = self.sender()
if act == self._cut:
self.statusBar.showMessage(act.text(), 2000)
#第二种方式不需要声明pySlot,因为用的不是字符串
def onTrigger2(self):
self.doTrigger()
#第一种连接方式,必须声明pyqtSlot
@pyqtSlot()
def onTrigger(self):
self.doTrigger()
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.resize(600, 400)
mw.show()
sys.exit(app.exec_())