信号与槽是Qt特有的机制。 pyqt5的信号与槽官方地址:信号与槽 UI界面如下:
思路: 按下对应按键显示对应信号文字。 主要说自定义信号,qt控件自带信号就不说了。 信号定义需要调用库:
QObejcet,pyqtSignal
from PyQt5.QtCore import QObject, pyqtSignal
类内部信号与槽:
Class_signal = pyqtSignal(str)
槽:
def Class_signal_print(self,singnal_str):
self.textBrowser.setText(singnal_str)
信号发送:
def pushbutton_clicked(self):
self.Class_signal.emit("类内部信号")
信号与槽连接:
self.Class_signal.connect(self.Class_signal_print)
类之间信号调用: 类定义:
class m_signal(QObject):
Class_signal_other = pyqtSignal(str)
def signal_emit(self):
self.Class_signal_other.emit("类之间信号")
信号定义:
Class_signal_other = pyqtSignal(str)
信号发送:
self.M_signal = m_signal()
def pushbutton_2_clicked(self):
self.M_signal.signal_emit()
def signal_emit(self):
self.Class_signal_other.emit("类之间信号")
槽函数:
def Class_signal_other_print(self,singnal_str):
self.textBrowser.setText(singnal_str)
信号与槽连接:
self.M_signal.Class_signal_other.connect(self.Class_signal_other_print)
信号的多态: 信号的定义:
Class_signature = pyqtSignal([str],[int])
信号的发送:
def Class_signature_1_print(self,singnal_str):
self.textBrowser.setText(singnal_str+":"+str(type(singnal_str)))
def pushButton_4_clicked(self):
self.Class_signature[int].emit(156277)
槽函数:
def Class_signature_1_print(self,singnal_str):
self.textBrowser.setText(singnal_str+":"+str(type(singnal_str)))
def Class_signature_2_print(self,singnal_int):
self.textBrowser.setText("信号_2 "+":"+str(singnal_int)+":"+str(type(singnal_int)))
信号与槽的连接:
self.Class_signature[str].connect(self.Class_signature_1_print)
self.Class_signature[int].connect(self.Class_signature_2_print)
源代码: 源代码