我 的 个 人 主 页: 失心疯的个人主页
入 门 教 程 推 荐 : Python零基础入门教程合集
虚 拟 环 境 搭 建 : Python项目虚拟环境(超详细讲解)
PyQt5 系 列 教 程: Python GUI(PyQt5)文章合集
Oracle数据库教程: Oracle数据库文章合集
优 质 资 源 下 载 : 资源下载合集
自定义信号传递数据
signal_name = pyqtSignal(数据类型)
# 例如
rightClicked = pyqtSignal(str)
# 在发射信号的时候,可以传递一个字符串类型的数据
rightClicked = pyqtSignal(int)
# 在发射信号的时候,可以传递一个整数类型的数据
self.rightClicked.emit('要传递的数据')
from PyQt5.Qt import *
import sys
class MyBtn(QPushButton):
rightClicked = pyqtSignal(str)
def mousePressEvent(self, evt) -> None:
super(MyBtn, self).mousePressEvent(evt)
if evt.button() == Qt.RightButton:
self.rightClicked.emit('这是要传递的字符串数据')
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('传递数据的自定义信号')
self.resize(500, 300)
self.widget_list()
def widget_list(self):
self.add_widget()
def add_widget(self):
btn = MyBtn('测试按钮',self)
btn.rightClicked.connect(lambda content: print('鼠标右键被按下了', content))
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Windows()
window.show()
sys.exit(app.exec_())
signal_name = pyqtSignal([数据类型1],[数据类型2])
# 示例
rightClicked = pyqtSignal([str],[int])
self.rightClicked[str].emit('这是要传递的str类型数据')
self.rightClicked[int].emit(8888)
from PyQt5.Qt import *
import sys
class MyBtn(QPushButton):
rightClicked = pyqtSignal([str], [int])
def mousePressEvent(self, evt) -> None:
super(MyBtn, self).mousePressEvent(evt)
if evt.button() == Qt.RightButton:
self.rightClicked[str].emit('这是要传递的字符串数据')
self.rightClicked[int].emit(888)
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('传递数据的自定义信号')
self.resize(500, 300)
self.widget_list()
def widget_list(self):
self.add_widget()
def add_widget(self):
btn = MyBtn('测试按钮',self)
btn.rightClicked.connect(lambda content: print('鼠标右键被按下了1', content))
btn.rightClicked[str].connect(lambda content: print('鼠标右键被按下了2', content))
btn.rightClicked[int].connect(lambda content: print('鼠标右键被按下了3', content))
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Windows()
window.show()
sys.exit(app.exec_())
signal_name pyqtSignal(数据类型1, 数据类型2...)
signal_name = pyqtSignal([数据类型1],[数据类型2],[数据类型3, 数据类型4...]...)
# 例如
rightClicked = pyqtSignal(str, int)
rightClicked = pyqtSignal([str], [int], [int, str])
rightClicked = pyqtSignal(list)
self.signal_name.emit(数据3, 数据4...)
self.signal_name[数据类型3, 数据类型4...].emit(数据3, 数据4...)
from PyQt5.Qt import *
import sys
class MyBtn(QPushButton):
rightClicked = pyqtSignal(str, int)
def mousePressEvent(self, evt) -> None:
super(MyBtn, self).mousePressEvent(evt)
if evt.button() == Qt.RightButton:
self.rightClicked.emit('这是要传递的字符串数据', 888)
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('传递数据的自定义信号')
self.resize(500, 300)
self.widget_list()
def widget_list(self):
self.add_widget()
def add_widget(self):
btn = MyBtn('测试按钮',self)
btn.rightClicked.connect(lambda v1, v2: print('鼠标右键被按下了1', v1, v2))
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Windows()
window.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class MyBtn(QPushButton):
rightClicked = pyqtSignal([str], [int], [int, str])
def mousePressEvent(self, evt) -> None:
super(MyBtn, self).mousePressEvent(evt)
if evt.button() == Qt.RightButton:
self.rightClicked[str].emit('这是要传递的字符串数据')
self.rightClicked[int].emit(888)
self.rightClicked[int, str].emit(888, '这是要传递的字符串字符2')
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('传递数据的自定义信号')
self.resize(500, 300)
self.widget_list()
def widget_list(self):
self.add_widget()
def add_widget(self):
btn = MyBtn('测试按钮',self)
btn.rightClicked.connect(lambda content: print('鼠标右键被按下了1', content))
btn.rightClicked[str].connect(lambda content: print('鼠标右键被按下了2', content))
btn.rightClicked[int].connect(lambda content: print('鼠标右键被按下了3', content))
btn.rightClicked[int, str].connect(lambda v1,v2: print('鼠标右键被按下了3', v1,v2))
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Windows()
window.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class MyBtn(QPushButton):
rightClicked = pyqtSignal(list)
def mousePressEvent(self, evt) -> None:
super(MyBtn, self).mousePressEvent(evt)
if evt.button() == Qt.RightButton:
self.rightClicked.emit(['a', 1, 'b'])
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('传递数据的自定义信号')
self.resize(500, 300)
self.widget_list()
def widget_list(self):
self.add_widget()
def add_widget(self):
btn = MyBtn('测试按钮',self)
btn.rightClicked.connect(lambda li: print('鼠标右键被按下了1', li))
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Windows()
window.show()
sys.exit(app.exec_())