Ai
1 Star 0 Fork 0

Shadow3D/PySide6-examples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
003-signal_slot.py 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
ShadowThreeD 提交于 2022-07-24 20:07 +08:00 . 1. add example about signal and slot in 003
""" code1: Using signal and slot
import sys
from PySide6.QtWidgets import QApplication, QToolButton, QLineEdit
app = QApplication([])
lineEdit = QLineEdit()
button = QToolButton()
button.clicked.connect(lineEdit.clear)
lineEdit.show()
button.show()
app.exec()
"""
""" code2: DIY slot
import sys
from PySide6.QtWidgets import QApplication, QPushButton
from PySide6.QtCore import Slot
@Slot()
def function():
print("The 'function' has been called!")
app = QApplication([])
button = QPushButton("Call function")
button.clicked.connect(function)
button.show()
sys.exit(app.exec())
"""
#""" code3: DIY signal and slot
import sys
from PySide6.QtWidgets import QApplication, QPushButton
from PySide6.QtCore import QObject, Signal, Slot
class Communicate(QObject):
# create two new signals on the fly: one will handle
# int type, the other will handle strings
speak = Signal((float,), (int,), (str,))
def __init__(self, parent=None):
super().__init__(parent)
self.speak[float].connect(self.say_something)
self.speak[int].connect(self.say_something)
self.speak[str].connect(self.say_something)
# define a new slot that receives a C 'int' or a 'str'
# and has 'say_something' as its name
@Slot(float)
@Slot(int)
@Slot(str)
def say_something(self, arg):
if isinstance(arg, float):
print("This is a float:", arg)
elif isinstance(arg, int):
print("This is a number:", arg)
elif isinstance(arg, str):
print("This is a string:", arg)
if __name__ == '__main__':
app = QApplication(sys.argv)
someone = Communicate()
# emit 'speak' signal with diff argments.
# we have to specify the str as int is the default
someone.speak[float].emit(10.2)
someone.speak[int].emit(10.2)
someone.speak[str].emit("Hello everyone!")
#"""
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ShadowThreeD/pyside6-examples.git
git@gitee.com:ShadowThreeD/pyside6-examples.git
ShadowThreeD
pyside6-examples
PySide6-examples
master

搜索帮助