Objects created from a QtCore.QObject can emit signals. In the following example we will see how we can emit custom signals.

#!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial In this example, we show how to emit a
signal. author: Jan Bodnar
website: zetcode.com
last edited: January 2015
""" import sys
from PyQt4 import QtGui, QtCore class Communicate(QtCore.QObject): closeApp = QtCore.pyqtSignal() class Example(QtGui.QMainWindow): def __init__(self):
super(Example, self).__init__() self.initUI() def initUI(self): self.c = Communicate()
self.c.closeApp.connect(self.close) self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Emit signal')
self.show() def mousePressEvent(self, event): self.c.closeApp.emit() def main(): app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main()

We create a new signal called closeApp. This signal is emitted during a mouse press event. The signal is connected to the close() slot of the QtGui.QMainWindow.

class Communicate(QtCore.QObject):

    closeApp = QtCore.pyqtSignal()

A signal is created with the QtCore.pyqtSignal() as a class attribute of the external Communicate class.

self.c.closeApp.connect(self.close)

The custom closeApp signal is connected to the close() slot of the QtGui.QMainWindow.

def mousePressEvent(self, event):

    self.c.closeApp.emit()

When we click on the window with a mouse pointer, the closeApp signal is emitted. The application terminates.

Emitting signals的更多相关文章

  1. [Repost]Events and Signals in PyQt4

    Reference:http://zetcode.com/gui/pyqt4/eventsandsignals/ Events and Signals in PyQt4 In this part of ...

  2. 词频统计_输入到文件_update

    /* 输入文件见337.in.txt 输出文件见338.out.txt */ #include <iostream> #include <cctype> #include &l ...

  3. API Design Principles -- QT Project

    [the original link] One of Qt’s most reputed merits is its consistent, easy-to-learn, powerfulAPI. T ...

  4. QT分析之网络编程

    原文地址:http://blog.163.com/net_worm/blog/static/127702419201002842553382/ 首先对Windows下的网络编程总结一下: 如果是服务器 ...

  5. Qt qml的软件架构设计

    google: qt qml application architecture 有很多资源. 1 https://www.ics.com/blog/multilayered-architecture- ...

  6. event & signals & threads

    The Event Systemhttp://doc.qt.io/qt-4.8/eventsandfilters.html Each thread can have its own event loo ...

  7. How Qt Signals and Slots Work(感觉是通过Meta根据名字来调用)

    Qt is well known for its signals and slots mechanism. But how does it work? In this blog post, we wi ...

  8. 从发布-订阅模式谈谈 Flask 的 Signals

    发布-订阅模式 发布-订阅模式,顾名思义,就像大家订报纸一样,出版社发布不同类型的报纸杂志不同的读者根据不同的需求预定符合自己口味的的报纸杂志,付费之后由邮局安排人员统一派送. 上面一段话,提到了发布 ...

  9. Flask备注二(Configurations, Signals)

    Flask备注二(Configuration, Signals) Flask是一个使用python开发Web程序的框架.依赖于Werkzeug提供完整的WSGI支持,以及Jinja2提供templat ...

随机推荐

  1. [BZOJ4870][六省联考2017]组合数问题(组合数动规)

    4870: [Shoi2017]组合数问题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 748  Solved: 398[Submit][Statu ...

  2. 基于Java 生产者消费者模式(详细分析)

    Java 生产者消费者模式详细分析 本文目录:1.等待.唤醒机制的原理2.Lock和Condition3.单生产者单消费者模式4.使用Lock和Condition实现单生产单消费模式5.多生产多消费模 ...

  3. PHP生成短网地址

    思路: 1)将长网址md5生成32位签名串,分为4段, 每段8个字节; 2)对这四段循环处理, 取8个字节, 将他看成16进制串与0x3fffffff(30位1)与操作, 即超过30位的忽略处理; 3 ...

  4. C#中&和&&,|和||区别

    当两者都为逻辑运算符时. 其实没什么差别. &&和||当已经确定结果时,不会对第二个操作数求值.也不知道什么情况会用到这个差别.做个笔记好了. http://blog.csdn.net ...

  5. python画激活函数图像

    导入必要的库 import math import matplotlib.pyplot as plt import numpy as np import matplotlib as mpl mpl.r ...

  6. JAVA容器-模拟ArrayList的底层实现

    概述 ArrayList实质上就是可变数组的实现,着重理解:add.get.set.remove.iterator的实现,我们将关注一下问题. 1.创建ArrayList的时候,默认给数组的长度设置为 ...

  7. keystone 命令简要说明

    catalog: keystone catalog 可以显示所有已有的service keystone catalog --service service-type 显示某个service信息 end ...

  8. mysqlslap

    常用参数[options]详细介绍: --concurrency代表并发数量,多个可以用逗号隔开.例如:--concurrency=50,200,500 --engines代表要测试的引擎,可以有多个 ...

  9. js继承——从创建对象开始

    从创建对象开始 创建对象的简单方法就是:使用Object构造函数或者对象字面量.但这两个方法有个缺点:创建相同对象,要编写大量重复代码. 为了避免这个问题——>工厂模式:用函数封装以特定接口创建 ...

  10. Android如何检查对象的类型

    The instanceof operator compares an object to a specified type. You can use it to test if an object ...