当前位置:网站首页>QT5知识:信号和槽的一些要点
QT5知识:信号和槽的一些要点
2022-06-23 11:53:00 【无水先生】
一、信号通过emit与槽传递参数
如果信号与槽为独立的两个类,初学者会发现两个类之间的参数是不能随便互相访问的,即使他们都是继承同一个父类也同样不能互相访问。这时候函数的传递就是emit完成的。下文将描述这种实现原理。

如上如图实现了A和B两个类的定义,在A中定义一个信号源;在B中有若干函数,其中一个是接收A中信号的函数。什么时候实现信号源和接收函数的连接?是在A和B都实例化以后,比如A和B处于共同的环境C中被实例化为As和Bs,那么C中通过语句As.pySignal.connect(Bs.function)实现两个对象的连接。注意:不可在类class A和class B中实现两者的连接。
下面通过简单代码阐述这种原理:
二、类间程序代码
import sys
from time import time
from PyQt5.QtCore import *
class A(QObject):
namechenged = pyqtSignal(str)
def init(self,name=‘hiu’):
QObject.init(self)
self.setname(name)
# 定义一个发射信号的方法,name为信号所带的参数
def setname(self,name):
self.__name=name
self.namechenged.emit(self.__name) # 这里是对应信号namechanged的槽处理函数,name为来自信号的参数
class B(QObject):
def nameset(self, name):
print(“Hello World!”,name)
if name == “main”:
app = QCoreApplication(sys.argv)
a = A()
b = B()
# 将信号和槽连接
a.namechenged.connect(b.nameset)
a.setname(“dash”)
# 调用信号发射函数
sys.exit(app.exec_())四、类内程序代码
如果是在一类内的不同组件之间进行这种联动,可以参照下面代码,一个信号可以对应多个槽响应:
import sys
from PyQt5.QtCore import (Qt, pyqtSignal)
from PyQt5.QtWidgets import (QWidget, QLCDNumber, QSlider,
QVBoxLayout, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def printLabel(self, str):
print(str)
def logLabel(self, str):
'''log to a file'''
pass
def initUI(self):
lcd = QLCDNumber(self)
sld = QSlider(Qt.Horizontal, self)
vbox = QVBoxLayout()
vbox.addWidget(lcd)
vbox.addWidget(sld)
self.setLayout(vbox)
#redundant connections
sld.valueChanged.connect(lcd.display)
sld.valueChanged.connect(self.printLabel)
sld.valueChanged.connect(self.logLabel)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Signal & slot')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())五、有些注意事项
5.1 QT4和QT5的使用有所不同-emit()
QT4:对于emit使用如下:
class Server(QTcpServer):
updateServer= pyqtSignal(list)
下文:
self.emit(SIGNAL("updateServer(QString,int)"),msg,length)
出现: AttributeError: 'TCPWindow' object has no attribute 'connect' 这个问题说明了PyQT5不在支持PyQT4的链接信号槽方式!
上面这个需要改成
self.updateServer(str,int).emit(msg,length)
注意:此处(str,int)限定了emit的参数 (msg,length)
5.2 QT4和QT5关于string的问题
注意PyQt4还是有QString这个东西的,PyQt5 找不到了,可以直接用 str 来替代,对于QString::number 都是直接可以使用 str() 来取代,可以说很方便,更加适应Python的风格。
对于connect使用如下(以pushButton为例):
self.connect(self.pb_build_tcp,SIGNAL("released()"),self.CreateServer)
改成
self.pb_build_tcp.released.connect(self.CreateServer)
边栏推荐
- 电脑坏了,换了台电脑,装node环境的时候出了问题,报错URL not defined
- At 14:00 today, 12 Chinese scholars started ICLR 2022
- [cloud native & microservice viii] source code analysis of weightedresponsetimerule of ribbon load balancing strategy (response time weighting)
- 过采样系列二:傅里叶变换与信噪比
- [cloud based co creation] overview of the IOT of Huawei cloud HCIA IOT v2.5 training series
- 开源之夏中选名单已公示,基础软件领域成为今年的热门申请
- ESP32-C3入门教程 问题篇⑧——blufi_example.c:244: undefined reference to `esp_ble_gap_start_advertising
- 2022工具钳工(初级)考试练习题模拟考试平台操作
- 六维图剖析:中国建筑集团有限公司企业成长性分析
- Learning notes sweep crawler framework
猜你喜欢

利用XtraDiagram.DiagramControl进行流程图形的绘制和控制

go-zero微服务实战系列(六、缓存一致性保证)

華為雲如何實現實時音視頻全球低時延網絡架構

Meta称英安全法可能“扫描所有私人信息” 或侵犯隐私

得物多活架构设计之路由服务设计

【ML】QuantileRegressor
![Openharmony application development [01]](/img/b1/1e37cecd3d3f9e46444c202cfb1b99.png)
Openharmony application development [01]
[email protected] HDMI2.0光端机 HDMI高清视频光端机"/>4K-HDMI光端机1路[email protected] HDMI2.0光端机 HDMI高清视频光端机

蓝桥杯单片机(一)——关闭外设及熄灭LED

Rancher 2.6 new monitoring QuickStart
随机推荐
[comprehensive written test questions] 30 Concatenate substrings of all words
Meta称英安全法可能“扫描所有私人信息” 或侵犯隐私
Necessary software for automation or electrical specialty
CIFAR公开第二阶段泛加拿大AI战略
Surprise! Amd acquires Xilinx with USD 35billion!
Is there any regulation on the redemption time of financial products?
坦然面对未来,努力提升自我
Signature analysis of app x-zse-96 in a Q & a community
1路百兆光纤收发器1百兆光1百兆电桌面式以太网光纤收发器内置电源
4路电话+1路千兆以太网4路PCM电话光端机
How to use note taking software flowus and note for interval repetition? Based on formula template
Getting started with redis - Chapter 4 - data structures and objects - jump table
汉源高科1路千兆光口转4路千兆以太网电口千兆1光4电光纤收发器
Does the inductance have polarity?
Where is the safest and most formal way to open an account at present?
Mysql, how to calculate the maximum value using stored procedures
蓝桥杯单片机(一)——关闭外设及熄灭LED
理财产品的赎回时间有规定吗?
在flinksql中 kafka流表跟mysql 纬度流表做left join,根据I’d做关联,假
Leetcode 1209. Delete all adjacent duplicates II in the string (not in the initial version)