您的位置:知识库 » 手机开发

PySide QtCore.Signal帮助手册

作者: meegoq.com  来源: MeeGo中文论坛-米趣网  发布时间: 2011-01-31 13:00  阅读: 5612 次  推荐: 0   原文链接   [收藏]  

  本文主要是翻译了Signal的文档,感兴趣的同学,可以参见 PySide的官方文档。翻译不足之处,还请指正。

  函数用法

def connect (receiver)
def disconnect (receiver)
def emit (*args)

  详细描述
  Signal类提供了使用符合python语法习惯的方法来定义以及连接Qt信号。

  PySide改造了PyQt的新的信号与槽方法,除了下面提到的特别情况,PySide的实现可以与PyQt 4.5实现功能兼容。

  使用QtCore.Signal()定义新信号
  PySide自动为Qt内置的信号定义了信号。使用QtCore.Signal()工厂方法定义新的信号为类的属性。

  QtCore.Signal()接受若干与信号签名相关的参数类型。每个类型可以是Python类型对象或者C++类型的字符串名称。同时,每个参数可以是类型参数的序列。本例中,每个序列定义了不同信号重载的签名。第一个重载被设置为默认。

  也可以选择性地提供一个参数 name 给QtCore.Signal(),其值可以设置信号的名称。信号发射时,将使用类属性的名字。

  下面,显示一系列定义信号的方法:

from PySide import QtCore

class Foo(QtCore.QObject):
# Define a new signal called 'trigger' that has no arguments.
trigger = QtCore.pyqtSignal()

def connect_and_emit_trigger(self):
# Connect the trigger signal to a slot.
self.trigger.connect(self.handle_trigger)

# Emit the signal.
self.trigger.emit()

def handle_trigger(self):
# Show that the slot has been called.
print "trigger signal received"

  新信号只应该在QObject子类中定义。

  使用这个方法定义的新信号将会自动添加入类的QMetaObject中。这意味着,它们将在Qt Designer中出现,而且也可以使用QMetaObject API反射获取。

  连接、断开以及发射信号
  信号与槽之间可以用Signal.connect()和Signal.disconnect()方法进行连接和断开,或者使用Signal.emit()方法发射信号。

  下面的代码演示如何定义、连接以及发射没有参数的信号

from PySide import QtCore

class Foo(QtCore.QObject):
# Define a new signal called 'trigger' that has no arguments.
trigger = QtCore.pyqtSignal()

def connect_and_emit_trigger(self):
# Connect the trigger signal to a slot.
self.trigger.connect(self.handle_trigger)

# Emit the signal.
self.trigger.emit()

def handle_trigger(self):
# Show that the slot has been called.
print "trigger signal received"

  下面则演示连接重载的信号

from PySide import QtGui

class Bar(QtGui.QComboBox):

def connect_activated(self):
# The PyQt documentation will define what the default overload is.
# In this case it is the overload with the single integer argument.
self.activated.connect(self.handle_int)

# For non-default overloads we have to specify which we want to
# connect. In this case the one with the single string argument.
# (Note that we could also explicitly specify the default if we
# wanted to.)
self.activated[str].connect(self.handle_string)

def handle_int(self, index):
print "activated signal passed integer", index

def handle_string(self, text):
print "activated signal passed string", text

  使用关键字参数连接信号
       在创建对象并想连接信号的时候,把槽作为关键字参数的值传递给构造函数。下面的代码实际上是等价的。

act = QtGui.QAction("Action", self)
act.triggered.connect(self.on_triggered)

act
= QtGui.QAction("Action", self, triggered=self.on_triggered)

  Signal.connect(receiver[, type=Qt.AutoConnection])
  在singal和receiver之间创立连接,receiver可以是Python函数,或者槽或信号。
  Signal.disconnect(receiver)
  断开singal与receiver间的连接。receiver同上。
  Signal.emit(*args)
  args是可选的参数序列,传递给任何已连接的槽。

0
0
 
标签:PySide

手机开发热门文章

    手机开发最新文章

      最新新闻

        热门新闻