本文主要介绍在PySide中如何使用信号和槽。传统的使用方式已经在参考文档里给出,我们的重点是解释如何使用新的代码风格来操作信号、槽。
PyQt中使用信号、槽的新代码风格是在PyQt v4.5中介绍的,这个风格的主要目的是为Python程序员们提供一个符合Python风格的方式。
传统方式:SINGAL和SLOT
QtCore.SIGNAL和QtCore.SLOT是Python使用Qt信号、槽传送机制的接口。这就是我们所说的旧方式。
下面这个例子使用了QPushButton的点击信号,而连接方法并非符合python习惯的语法。它需要通知对象,并将它的信号和另外个槽连接。
...
def someFunc():
print "someFunc has been called!"
...
button = QtGui.QPushButton("Call someFunc")
QtCore.QObject.connect(button, QtCore.SIGNAL('clicked()'), someFunc)
...
新风格:Signal()和Slot()
新风格使用了不同的语法创建并连接信号、槽。前面的例子可以重写如下:
...
def someFunc():
print "someFunc has been called!"
button = QtGui.QPushButton("Call someFunc")
button.clicked.connect(someFunc)
...
使用QtCore.Singal()
信号可以使用QtCore.Signal()定义。Python类型和C类型都可以作为参数传递给它。假如你需要重载它,只需要用无组或列表地方式传递类型。
另外,它也可以接受命名参数(named argument) name来定义信号的名称。如果没有传递name,那么信号将会使用赋值变量的名称。
第二个例子中使用了一组方式来显示如何使用QtCore.Signal()
注意:信号只能在QObject的子类内定义,这种方式下信号的信息将会加入 QMetaObject结构中。
使用 QtCore.Slot()
槽可以用QtCore.Slot()赋值或者重载。同样,想定义一个签名仅仅需要传递给QtCore.Singal()一些类型。与Signal()类不同的是,想重载一个函数,你并不需要传递任何的无级或者列表,你需要做的是为每个不同的签名定义一个不同的装饰。如果没看懂没有关系,下面的例子将会让你明了。
另外一个区别是关键字。Slot()接受一个name和一个result. result关键字定义的是返回值,可以是C或者Python的类型。name则与Signal中的一样。如果没有传递name,则使用函数的名字。
一组例子:
下面有一组例子,用来显示如何在PySide中定义及连接信号、槽。有一些例子比较简单,有一些则比较复杂。
1. Hello World: 基本的例子,显示不使用任何参数的情况下连接信号和槽
代码
#!/usr/bin/env python
import sys
from PySide import QtCore, QtGui
# define a function that will be used as a slot
def sayHello():
print 'Hello world!'
app = QtGui.QApplication(sys.argv)
button = QtGui.QPushButton('Say hello!')
# connect the clicked signal to the sayHello slot
button.clicked.connect(sayHello)
button.show()
sys.exit(app.exec_())
2. 接着,我们加了一些参数。是从 Hello World版本修改的。槽中添加了一些参数,同时创建了一个新的信号
代码
#!/usr/bin/env python
import sys
from PySide import QtCore
# define a new slot that receives a string and has
# 'saySomeWords' as its name
@QtCore.Slot(str)
def saySomeWords(words):
print words
class Communicate(QtCore.QObject):
# create a new signal on the fly and name it 'speak'
speak = QtCore.Signal(str)
someone = Communicate()
# connect signal and slot
someone.speak.connect(saySomeWords)
# emit 'speak' signal
someone.speak.emit("Hello everybody!")
3. 添加一些重载。简单地修改了前一个例子。下面是重载修饰符
代码
#!/usr/bin/env python
import sys
from PySide import QtCore
# define a new slot that receives a C 'int' or a 'str'
# and has 'saySomething' as its name
@QtCore.Slot(int)
@QtCore.Slot(str)
def saySomething(stuff):
print stuff
class Communicate(QtCore.QObject):
# create two new signals on the fly: one will handle
# int type, the other will handle strings
speakNumber = QtCore.Signal(int)
speakWord = QtCore.Signal(str)
someone = Communicate()
# connect signal and slot properly
someone.speakNumber.connect(saySomething)
someone.speakWord.connect(saySomething)
# emit each 'speak' signal
someone.speakNumber.emit(10)
someone.speakWord.emit("Hello everybody!")
4. 最后一个例子,使用了槽重载以及一些复杂的信号连接及发射。
代码
#!/usr/bin/env python
import sys
from PySide import QtCore
# define a new slot that receives an C 'int' or a 'str'
# and has 'saySomething' as its name
@QtCore.Slot(int)
@QtCore.Slot(str)
def saySomething(stuff):
print stuff
class Communicate(QtCore.QObject):
# create two new signals on the fly: one will handle
# int type, the other will handle strings
speak = QtCore.Signal((int,), (str,))
someone = Communicate()
# connect signal and slot. As 'int' is the default
# we have to specify the str when connecting the
# second signal
someone.speak.connect(saySomething)
someone.speak[str].connect(saySomething)
# emit 'speak' signal with different arguments.
# we have to specify the str as int is the default
someone.speak.emit(10)
someone.speak[str].emit("Hello everybody!")
PyQt兼容模式:
PyQt使用了另外一种方式。为了将PyQt脚本转而使用PySide运行,只需要使用下面的代码进行修改。
from PySide.QtCore import Signal as pyqtSignal
from PySide.QtCore import Slot as pyqtSlot
或者
QtCore.pyqtSignal = QtCore.Signal
QtCore.pyqtSlot = QtCore.Slot