信号与槽
一个信号连接一个槽
按钮的clicked信号与自定义的change_text()槽函数连接起来
import sys
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
self.btn=QPushButton('开始',self)
self.btn.clicked.connect(self.change_text)
def change_text(self):
if self.btn.text()=='开始':
self.btn.setText('停止')
else:
self.btn.setText('开始')
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())
每个控件都有相应内置信号,比如QPushButton控件有clicked、pressed等
信号可以传值
import sys
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
self.resize(180,30)
line=QLineEdit(self)
line.textChanged.connect(self.show_text)
# a是show_text的一个参数,textChanged信号携带的值会传给这个参数
def show_text(self,a):
print(a)
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())
当信号无法传值而又需要传值时,可以使用lambda匿名函数
import sys
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
btn=QPushButton('开始',self)
btn.clicked.connect(lambda:btn.setText('停止'))
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())
一个信号连接多个槽
信号发射一次,调用多个槽函数
import sys
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
self.btn=QPushButton('开始',self)
self.btn.clicked.connect(self.change_text)
self.btn.clicked.connect(self.change_size)
def change_text(self):
if self.btn.text()=='开始':
self.btn.setText('停止')
else:
self.btn.setText('开始')
def change_size(self):
self.btn.resize(150,30)
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())
多个信号连接一个槽
import sys
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
self.btn=QPushButton('开始',self)
#按下
self.btn.pressed.connect(self.change_text)
#松开
self.btn.released.connect(self.change_text)
def change_text(self):
if self.btn.text()=='开始':
self.btn.setText('停止')
else:
self.btn.setText('开始')
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())
信号与信号连接
import sys
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
self.btn=QPushButton('开始',self)
self.btn.pressed.connect(self.btn.released)
self.btn.released.connect(self.change_text)
def change_text(self):
if self.btn.text()=='开始':
self.btn.setText('停止')
else:
self.btn.setText('开始')
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())
自定义信号
- 创建自定义信号
自定义信号通过
Signal
来创建
import sys
from PySide6.QtCore import *
from PySide6.QtWidgets import *
class Window(QWidget):
my_signal=Signal()
def __init__(self):
super(Window,self).__init__()
self.my_signal.connect(self.my_slot)
def my_slot(self):
print(self.width())
print(self.height())
# 鼠标按下事件
def mousePressEvent(self, event):
self.my_signal.emit()
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())
- 让自定义信号携带值
import sys
from PySide6.QtCore import *
from PySide6.QtWidgets import *
class Window(QWidget):
my_signal=Signal(int,int)
def __init__(self):
super(Window,self).__init__()
self.my_signal.connect(self.my_slot)
def my_slot(self,x,y):
print(x)
print(y)
# 鼠标按下事件
def mousePressEvent(self, event):
x=event.pos().x()
y=event.pos().y()
self.my_signal.emit(x,y)
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())
笔记笔者根据《Pyqt编程快速上手》将其中pyqt改为pyside整理而成,所有代码均经过验证