zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    细说javascripe事件传播流程
    由浅入深的讲述Get和Post的区别
    使用windowsAPI 加载shellcode
    从PE资源加载和执行Shellcode
    DLL注入
    shellcode注入原理
    asp.net学习--ashx一句话木马
    asp.net学习--asmx一句话木马
    asp.net学习--svc一句话木马
    php--莫客服系统代码审计(已申请CNVD)
  • 原文地址:https://www.cnblogs.com/hushaojun/p/4435570.html
Copyright © 2011-2022 走看看