• 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.

  • 相关阅读:
    岁月
    唯美励志古风
    活着
    走进华夏统一
    使用Fiddler测试WebApi接口
    深入了解正则表达式
    Linux 学习_ssh(secure shell)
    PHP使用mysql扩展操作数据库
    给自己的网站加点情趣,常用的javaScript效果
    三层架构的基础知识
  • 原文地址:https://www.cnblogs.com/hushaojun/p/4435570.html
Copyright © 2020-2023  润新知