Emitting signals

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.

时间: 2024-07-30 22:04:45

Emitting signals的相关文章

[Repost]Events and Signals in PyQt4

Reference:http://zetcode.com/gui/pyqt4/eventsandsignals/ Events and Signals in PyQt4 In this part of the PyQt4 programming tutorial, we will explore events and signals occurring in applications. Events All GUI applications are event-driven. Events ar

自定义信号与槽

引自:<PyQt5官网Doc:Support for Signals and Slots><Qt5官网: Signals & Slots> Qt 对于大部分widget的常规操作,都预定义了一系列的 connect(),例如你按下一个按钮,至于动作的实现,只需要重写 On_Click_Button() 就能实现.这个过程就包括了内在的信号-槽连接.而这些关联动作已经在基类中配置好了,故而你不需要指定connect也可以实现动作. 但如果我们需要自定义一些信号和槽的连接动作呢

event &amp; signals &amp; threads

The Event Systemhttp://doc.qt.io/qt-4.8/eventsandfilters.html Each thread can have its own event loop. The initial thread starts its event loops using QCoreApplication::exec(); other threads can start an event loop using QThread::exec(). Qt signals (

vs2013 boost signals

#include "stdafx.h" #include <boost/signals2/signal.hpp> #include <iostream> using namespace std; void func() { std::cout << "Hello, world!" << std::endl; } void slots1() { cout << "slots1 call"

Django Signals 从实践到源码分析(转)

原文:http://foofish.net/blog/66/django-signals 当某个事件发生的时候,signal(信号)允许senders(发送者)用来通知receivers(接收者),通知receivers干嘛?你想要recivers干嘛就可以干嘛.这在多处代码 对同一个事件感兴趣的时候就有用武之地了. 比如:Django提供了一个built-in signal,叫django.core.signals.request_finished,这个signal会在一个 HTTP请求完成后

uva 467 - Synching Signals(暴力+数学)

题目连接:uva 467 - Synching Signals 题目大意:有n个红绿灯,给出红灯的时间t,那么该灯从0时刻开始就以2*t为周期绿黄红三灯交替,时间分别为t-5,5,t.问所这n个等从第一变为有一个灯不为绿灯开始,要多久才能变成所有的灯刚好都为绿灯.时间超过1小时输出unable to synch after one hour. 解题思路:一小时才3600秒,枚举秒数判断即可. #include <cstdio> #include <cstring> #include

zoj1986 Bridging Signals (dp,最长递增序列,LIS)

A - Bridging Signals Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status Practice ZOJ 1986 Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip factory. Once more the routing designer

Bridging signals

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1079    Accepted Submission(s): 704 Problem Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip factory. O

观察者模式与Boost.Signals

1)  观察者模式定义 略,各种设计模式的书上都有定义. 2)  观察者模式一般实现 观察者模式一般实现,都是“被观察者”保存一个“观察者”的列表,循环这个列表来通知“观察者”.代码,其中使用了boost的智能指针shared_ptr: [cpp] view plaincopy #include <iostream> #include <list> #include "boost/shared_ptr.hpp" using namespace std; usin