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. 【UOJ #104】【APIO 2014】Split the sequence

    http://uoj.ac/problem/104 此题的重点是答案只与切割的最终形态有关,与切割顺序无关. 设\(f(i,j)\)表示前\(i\)个元素切成\(j\)个能产生的最大贡献. \(f(i ...

  2. BZOJ4029 HEOI2015定价

    贪心. 每次将最后一个非零位加一判断即可. 一开始想少了,只关心把最后一位变成5了,其实可以都变的. #include<bits/stdc++.h> using namespace std ...

  3. 【DP】BZOJ1564- [NOI2009]二叉查找树(!!)

    [题目大意] 已知一个treap上每个节点的键值.权值和访问频率.现在可以修改一些节点的权值(可以修改为实数),需要付出k(k为定制)的额外代价.一个treap的总代价=∑(每个节点的访问频率*深度) ...

  4. seebug的反爬虫技术初探

    1.通过request库无法直接爬取,返回521 >>> import requests >>> req = requests.get('https://www.s ...

  5. CountDownLatch源码分析

    CountDownLatch.Semaphore(信号量)和ReentrantReadWriteLock.ReadLock(读锁)都采用AbstractOwnableSynchronizer共享排队的 ...

  6. Unity快捷键总结

    Shift+Alt+A  物体快速激活 Ctrl+P 开始 Ctrl+Shift+P 暂停 Ctrl+B  编译并运行 Z  Pivot/Center切换 X Local/Global切换

  7. zabbix install

    Auth: Jin Date: 20140714 用了5 6年的监控工具 http://zabbix.org/wiki/InstallOnCentOS_RHEL Server Install yum ...

  8. 解决WPF中重载Window.OnRender函数失效问题

    今天实验一个绘图算法的时候,偶然发现重载Window.OnRender的方法是没有效果的. public partial class MainWindow : Window { public Main ...

  9. Buck converter uses low-side PWM IC

    The most common switching-power topology is a buck converter, which efficiently transforms high volt ...

  10. Druid 配置_配置WebStatFilter

    https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_%E9%85%8D%E7%BD%AEWebStatFilter WebStatFilt ...