(3)PySide2 signal 和slot

版权声明:本文为博主原创文章,未经博主允许下请随便转载。 https://blog.csdn.net/god_wen/article/details/88092084

传统写法

import sys
from PySide2.QtWidgets import QApplication, QPushButton
from PySide2.QtCore import SIGNAL, QObject

def func():
    print("func has been called!")

app = QApplication(sys.argv)
button = QPushButton("Call func")
QObject.connect(button, SIGNAL ('clicked()'), func)
button.show()                                                                                             

sys.exit(app.exec_())

这种写法很复杂,通过静态函数和两个宏来完成signal 和slot的对接所以一点页不不pythonic,官方不推荐的写法

新的写法

import sys
from PySide2.QtWidgets import QApplication, QPushButton

def func():
 print("func has been called!")

app = QApplication(sys.argv)
button = QPushButton("Call func")
button.clicked.connect(func)
button.show()
sys.exit(app.exec_())

这个句法是官方推荐的。

自定义Signal 和Slot

下面定义Slot 和Signal 是最简单的

import sys
from PySide2 import QtCore, QtGui

# define a function that will be used as a slot
def sayHello():
 print 'Hello world!'

app = QtGui.QApplication(sys.argv)

button = QtGui.QPushButton('Say hello!')

# connect the clicked signal to the sayHello slot
button.clicked.connect(sayHello)
button.show()

sys.exit(app.exec_())

下面展示了如何用Wrapper来定义Slot,并且Signal和Slot开始处理类型变量了

import sys                                                                  
from PySide2.QtWidgets import QApplication, QPushButton                     
from PySide2.QtCore import QObject, Signal, Slot                            
                                                                            
app = QApplication(sys.argv)                                                
                                                                            
# define a new slot that receives a string and has                          
# 'saySomeWords' as its name                                                
@Slot(str)                                                                  
def say_some_words(words):                                                  
    print(words)                                                               
                                                                            
class Communicate(QObject):                                                 
 # create a new signal on the fly and name it 'speak'                       
 speak = Signal(str)                                                        
                                                                            
someone = Communicate()                                                     
# connect signal and slot                                                   
someone.speak.connect(say_some_words)                                         
# emit 'speak' signal                                                         
someone.speak.emit("Hello everybody!")

下面是重载版本

import sys                                                                  
from PySide2.QtWidgets import QApplication, QPushButton                     
from PySide2.QtCore import QObject, Signal, Slot                            
                                                                            
app = QApplication(sys.argv)                                                
                                                                            
# define a new slot that receives a C 'int' or a 'str'                      
# and has 'saySomething' as its name                                        
@Slot(int)                                                                  
@Slot(str)                                                                  
def say_something(stuff):                                                   
    print(stuff)                                                            
                                                                            
class Communicate(QObject):                                                 
    # create two new signals on the fly: one will handle                    
    # int type, the other will handle strings                               
    speak_number = Signal(int)                                              
    speak_word = Signal(str)                                                  
                                                                            
someone = Communicate()                                                     
# connect signal and slot properly                                          
someone.speak_number.connect(say_something)                                 
someone.speak_word.connect(say_something)                                   
# emit each 'speak' signal                                                  
someone.speak_number.emit(10)                                               
someone.speak_word.emit("Hello everybody!"))

pythinic版本的重载(我也第一次看见)

import sys
from PySide2.QtWidgets import QApplication, QPushButton
from PySide2.QtCore import QObject, Signal, Slot

app = QApplication(sys.argv)

# define a new slot that receives a C 'int' or a 'str'
# and has 'saySomething' as its name
@Slot(int)
@Slot(str)
def say_something(stuff):
    print(stuff)

class Communicate(QObject):
    # create two new signals on the fly: one will handle
    # int type, the other will handle strings
    speak = Signal((int,), (str,))

someone = Communicate()
# connect signal and slot. As 'int' is the default
# we have to specify the str when connecting the
# second signal
someone.speak.connect(say_something)
someone.speak[str].connect(say_something)

# emit 'speak' signal with different arguments.
# we have to specify the str as int is the default
someone.speak.emit(10)
someone.speak[str].emit("Hello everybody!")

一个对象发出信号

import sys                                                                  
from PySide2.QtCore import QObject, Signal                                  
                                                                            
# Must inherit QObject for signals                                          
class Communicate(QObject):                                                 
    speak = Signal()                                                        
              
    def __init__(self):                                                     
        super(Communicate, self).__init__()    
        self.speak.connect(self.say_hello)                             
                                                                            
    def speaking_method(self):                                              
        self.speak.emit()   

    def say_hello(self):
        print("Hello")                                                

                                                                            
someone = Communicate()                                                 
someone.speaking_method()

注意: 类的实例才能发射信号 ,类是不可以发射信号的

# Erroneous: refers to class Communicate, not an instance of the class
Communicate.speak.connect(say_something)
# raises exception: AttributeError: 'PySide2.QtCore.Signal' object has no attribute 'connect'

猜你喜欢

转载自blog.csdn.net/god_wen/article/details/88092084
今日推荐