PySide——Python图形化界面入门教程(四)

              ——创建自己的信号槽

              ——Creating Your Own Signals and Slots

翻译自:http://pythoncentral.io/pysidepyqt-tutorial-creating-your-own-signals-and-slots/

你不必局限于Qt widget提供的信号,你可以使用Signal类来创建自己的信号。下面是一个定义的简单例子:

 from PySide.QtCore import Signal
tapped = Signal()

然后,当对象需要触发信号的条件满足时,你可以使用信号的emit方法,来发出信号调用关联的槽。

thing.tapped.emit()

这样做有两个优点:第一,允许用户和你定义的对象随意交互;第二,让你的对象使用更加灵活,让自己的代码定义动作行为的影响。

一个简单的PySide信号例子

我们来定义一个简单的PunchingBag类,它只做一件事情,当punch被调用时,发出punched信号:

 from PySide.QtCore import QObject, Signal, Slot

 class PunchingBag(QObject):
''' Represents a punching bag; when you punch it, it
emits a signal that indicates that it was punched. '''
punched = Signal() def __init__(self):
# Initialize the PunchingBag as a QObject
QObject.__init__(self) def punch(self):
''' Punch the bag '''
self.punched.emit()

代码非常的简单:PunchingBag继承自QObject,所以它可以发出信号;它有一个称为punched的信号,不携带任何数据;并且他有一个仅仅发出punched信号的punch方法。

为了让PunchingBag更丰富一些,我们需要将它的punched信号和一个槽连接。槽简单的输出“Bag was punched”。

 @Slot()
def say_punched():
''' Give evidence that a bag was punched. '''
print('Bag was punched.') bag = PunchingBag()
# Connect the bag's punched signal to the say_punched slot
bag.punched.connect(say_punched) # Punch the bag 10 times
for i in range(10):
bag.punch()

携带数据的PySide信号

创建信号可以完成一个非常有用的事情——携带数据。例如,你可以创建一个携带整数或者字符串的信号:

updated = Signal(int)
updated = Signal(str)

这个数据类型可以是任何Python的类型名或定义了C++类型的字符串。因为教程不假设有任何C++的知识,故我们只使用Python类型。

例子:一个发送信号的圆

我们用x,y和r定义一个圆,x、y是圆中心的坐标,r是半径。我们想要当圆被改变大小时,发送一个信号resized;当圆被移动时,也发送一个信号moved。虽然我们可以在信号的槽中检测圆的大小和位置,但是使用信号发送这些信息会更加方便。

 from PySide.QtCore import QObject, Signal, Slot

 class Circle(QObject):
''' Represents a circle defined by the x and y
coordinates of its center and its radius r. '''
# Signal emitted when the circle is resized,
# carrying its integer radius
resized = Signal(int)
# Signal emitted when the circle is moved, carrying
# the x and y coordinates of its center.
moved = Signal(int, int) def __init__(self, x, y, r):
# Initialize the Circle as a QObject so it can emit signals
QObject.__init__(self) # "Hide" the values and expose them via properties
self._x = x
self._y = y
self._r = r @property
def x(self):
return self._x @x.setter
def x(self, new_x):
self._x = new_x
# After the center is moved, emit the
# moved signal with the new coordinates
self.moved.emit(new_x, self.y) @property
def y(self):
return self._y
@y.setter
def y(self, new_y):
self._y = new_y
# After the center is moved, emit the moved
# signal with the new coordinates
self.moved.emit(self.x, new_y) @property
def r(self):
return self._r @r.setter
def r(self, new_r):
self._r = new_r
# After the radius is changed, emit the
# resized signal with the new radius
self.resized.emit(new_r)

注意以下几点:

  • Circle继承自QObject所以可以发送信号
  • 同样的信号可以在不同地方发送

现在我们定义一些连接Circle的信号的槽。还记得我们上次提过的@Slot修饰符(decorator)吗?现在我们来看看如何接收携带了数据的信号。为了接收信号,我们简单的将其定义为与信号一样的结构。

 # A slot for the "moved" signal, accepting the x and y coordinates
@Slot(int, int)
def on_moved(x, y):
print('Circle was moved to (%s, %s).' % (x, y)) # A slot for the "resized" signal, accepting the radius
@Slot(int)
def on_resized(r):
print('Circle was resized to radius %s.' % r)

非常的简单直观。更多信息可以参考Python decorators,或者来学习这篇文章Python Decorators Overview。最后我们完成这个Circle,连接信号槽,移动并改变它的大小。

 c = Circle(5, 5, 4)

 # Connect the Circle's signals to our simple slots
c.moved.connect(on_moved)
c.resized.connect(on_resized) # Move the circle one unit to the right
c.x += 1 # Increase the circle's radius by one unit
c.r += 1

当运行脚本的时候,你的结果应该是:

Circle was moved to (6, 5).
Circle was resized to radius 5.

现在我们对信号和槽有了更深入的了解,可以准备使用一些更高级的widgets了。下一个教程开始讨论QListWidget和QListView,两种创建表框(list box)控件的方法。

By Ascii0x03

转载请注明出处:http://www.cnblogs.com/ascii0x03/p/5500933.html

PySide——Python图形化界面入门教程(四)的更多相关文章

  1. PySide——Python图形化界面入门教程(六)

    PySide——Python图形化界面入门教程(六) ——QListView和QStandardItemModel 翻译自:http://pythoncentral.io/pyside-pyqt-tu ...

  2. PySide——Python图形化界面入门教程(五)

    PySide——Python图形化界面入门教程(五) ——QListWidget 翻译自:http://pythoncentral.io/pyside-pyqt-tutorial-the-qlistw ...

  3. PySide——Python图形化界面入门教程(三)

    PySide——Python图形化界面入门教程(三) ——使用内建新号和槽 ——Using Built-In Signals and Slots 上一个教程中,我们学习了如何创建和建立交互widget ...

  4. PySide——Python图形化界面入门教程(二)

    PySide——Python图形化界面入门教程(二) ——交互Widget和布局容器 ——Interactive Widgets and Layout Containers 翻译自:http://py ...

  5. PySide——Python图形化界面入门教程(一)

    PySide——Python图形化界面入门教程(一) ——基本部件和HelloWorld 翻译自:http://pythoncentral.io/intro-to-pysidepyqt-basic-w ...

  6. PySide——Python图形化界面

    PySide——Python图形化界面 PySide——Python图形化界面入门教程(四) PySide——Python图形化界面入门教程(四) ——创建自己的信号槽 ——Creating Your ...

  7. python+pycharm+PyQt5 图形化界面安装教程

    python图形化界面安装教程 配置环境变量 主目录 pip所在目录,及script目录 更新pip(可选) python -m pip install --upgrade pip ps:更新出错一般 ...

  8. Oracle数据库及图形化界面安装教程详解

    百度云盘oracle数据库及图形化界面安装包 链接: https://pan.baidu.com/s/1DHfui-D2n1R6_ND3wDziQw 密码: f934 首先在电脑D盘(或者其他不是C盘 ...

  9. 如何使用python图形化界面wxPython

    GUI库主要有三类:tkinter,wxPython和PyQt5,下面主要是针对wxPython的使用说明. 下面的操作均在win10 + pycharm上进行 wxPython的安装: pip in ...

随机推荐

  1. echarts怎么使用(最最最最简单版)(本质canvas)

    echarts怎么使用(最最最最简单版)(本质canvas) 一.总结 一句话总结:外部扩展插件肯定要写js啊,不然数据怎么进去,不然宽高怎么设置.本质都是canvas嵌套在页面上,比如div中. 1 ...

  2. [Angular] Create a simple *ngFor

    In this post, we are going to create our own structure directive *ngFor. What it should looks like i ...

  3. Android 自定义View——自定义点击事件

    每个人手机上都有通讯录,这是毫无疑问的,我们通讯录上有一个控件,在通讯录的最左边有一列从”#”到”Z”的字母,我们通过滑动或点击指定的字母来确定联系人的位置,进而找到联系人.我们这一节就通过开发这个控 ...

  4. JS类型转换规则详解

    JS类型转换规则详解 一.总结 一句话总结:JS强制类型转换中的类型名强制类型转换和其它语言不同,是类型类的构造方法,Number(mix) 一句话总结(JS类型本质):因为js是弱类型语言,所以它相 ...

  5. BZOJ 2096 Pilots - 单调队列STL(deque)

    传送门 分析: 单调队列:维护两个递增.递减的队列,每次都加入新元素并更新,如果最大值(递减队首)-最小值(递增队首) > k,那么将最左段更新为前面两者中较前的那一个,并弹掉.用deque可以 ...

  6. SpringMVC大坑一枚:ContentNegotiatingViewResolver可能不利于SEO

    广大站长都有关注自己网站被搜索引擎收录的习惯,最近用百度.360等搜索引擎,查看了自己网站的一些情况,使用命令"site:fansunion.cn". 我发现了一些异常信息,不止一 ...

  7. c头文件(.h)的作用

    C语言的著作中,至今还没发现把.h文件的用法写的透彻的.在实际应用中也只能依葫芦画瓢,只知其然不知其所以然,甚是郁闷!闲来无事,便将搜集网络的相关内容整理一下,以便加深自己的理解 理论概述:.h中一般 ...

  8. scala 加载与保存xml文档

    package scala_enhance.xml import scala.xml.XML import scala.io.Source import jdk.internal.org.xml.sa ...

  9. iOS 取消多余tableView的横线的写法

    - (void)setExtraCellLineHidden: (UITableView *)tableView{ UIView *view =[ [UIView alloc]init]; view. ...

  10. qemu使用copy-on-write(COW)磁盘

    写时复制(copy-on-write,缩写COW)技术不会对原始的镜像文件做更改,变化的部分写在另外的镜像文件中,这种特性在qemu中只有QCOW格式支持,多个 COW 文件可以指向同一映像同时测试多 ...