当前位置: 首页 > news >正文

python之pyqt专栏7-信号与槽3

        在上一篇文章中python之pyqt专栏6-信号与槽2-CSDN博客中,我们可以了解到对象可以使用内置信号,这些信号来自于类定义或者继承过来的。我们可以对这些信号可以通过connect连接槽函数。

需求

        现在有一个需求,有两个UI界面“untitled.ui”和“untitled1.ui”,untitled.ui有一个lineEdit(行编辑)和一个button(按钮),untitled1.ui有一个Label。点击untitled.ui的button时,将行编辑的文本内容,设置为untitled1.uiLabel文本内容。

untitled.ui的对象列表

对象名类型
lineEditLlineEdit
pushButtonQPushButto

untitled1.ui的对象列表

对象名类型
labelQLabel

UI界面设置

untitled.ui  UI界面

       保存文件为untitled.ui

untitled1.ui  UI界面

        点击左上角“文件”->“新建”

        保存文件为untitled.ui 

         注:Qt Designer中,当有两个以上的UI编辑界面时,需要先选中的UI界面,再保存

 项目目录下“untitled.ui”“untitled1.ui”转换为“untitled.py”“untitled1.py”

main.py

# 导入sys模块
import sys
# PyQt6.QtWidgets模块中导入QApplication, QWidget
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtCore import QObjectimport untitled
import untitled1class MyMainForm(QWidget, untitled.Ui_Form):sendText = pyqtSignal(str)def __init__(self, parent=None):# 调用父类的构造函数super(MyMainForm, self).__init__(parent)# 调用继承Ui_Form过来的setupUi函数self.setupUi(self)self.pushButton.clicked.connect(self.btn_clicked)class MyMainForm1(QWidget, untitled1.Ui_Form):def __init__(self, parent=None):# 调用父类的构造函数super(MyMainForm1, self).__init__(parent)# 调用继承Ui_Form过来的setupUi函数self.setupUi(self)self.move(1200,320)# Press the green button in the gutter to run the script.
if __name__ == '__main__':# 实例化应用app = QApplication(sys.argv)# 实例化MyMainFormmyw = MyMainForm()myw.show()myw1 = MyMainForm1()myw1.show()myw.sendText.connect(myw1.deal_signal)# 启动应用程序的事件循环并等待用户交互,直到应用程序关闭。sys.exit(app.exec())

        防止两个窗口重叠,在MyMainForm1移动一下位置

self.move(1200,320)

  

        有两个窗口,建立了两个类MyMainFormMyMainForm1,它们分别继承于untitled.Ui_Formuntitled1.Ui_Form

        需要注意的是untitled.py与untitled1.py都有Ui_Form,为了区分Ui_Form来源,不能用如下代码,否者会被Ui_Form会被后面的取代

from untitled import Ui_Form
from untitled1 import Ui_Form

         正确书写应该是这样

import untitled
import untitled1
class MyMainForm(QWidget, untitled.Ui_Form):
class MyMainForm1(QWidget, untitled1.Ui_Form):

  问题

        在MyMainForm,button被点击时会发出clicked信号,如果用将buttonclicked信号,绑定槽函数,在这个槽函数里面可以实现获取lineEdit的文本内容,代码如下

self.pushButton.clicked.connect(self.btn_clicked)
    def btn_clicked(self):# 获取行编辑文本str = self.lineEdit.text()

        MyMainForm与MyMainForm1,它们是两个类,没有直接关系这个槽函数在MyMainForm中,不能修改MyMainForm1的label,也就是不能通过如下代码

    def btn_clicked(self):# 获取行编辑文本str = self.lineEdit.text()self.label.setText(str)

 自定义信号

          如果我们可以在untitled.pyUi_Form自定义一个信号(sendText),这个信号通过connect绑定untitled1.pyUi_Form类函数(deal_signal),那么它们就会建立关系。

 myw.sendText.connect(myw1.deal_signal)
修改代码如下

# 导入sys模块
import sys
# PyQt6.QtWidgets模块中导入QApplication, QWidget
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtCore import QObject, pyqtSignalimport untitled
import untitled1class MyMainForm(QWidget, untitled.Ui_Form):sendText = pyqtSignal(str)def __init__(self, parent=None):# 调用父类的构造函数super(MyMainForm, self).__init__(parent)# 调用继承Ui_Form过来的setupUi函数self.setupUi(self)self.pushButton.clicked.connect(self.btn_clicked)def btn_clicked(self):# 获取行编辑文本str = self.lineEdit.text()self.sendText.emit(str)class MyMainForm1(QWidget, untitled1.Ui_Form):def __init__(self, parent=None):# 调用父类的构造函数super(MyMainForm1, self).__init__(parent)# 调用继承Ui_Form过来的setupUi函数self.setupUi(self)self.move(1200,320)def deal_signal(self,str):self.label.setText(str)# Press the green button in the gutter to run the script.
if __name__ == '__main__':# 实例化应用app = QApplication(sys.argv)# 实例化MyMainFormmyw = MyMainForm()myw.show()myw1 = MyMainForm1()myw1.show()myw.sendText.connect(myw1.deal_signal)# 启动应用程序的事件循环并等待用户交互,直到应用程序关闭。sys.exit(app.exec())
      自定义信号过程
      1)导入 pyqtSignal类 
from PyQt6.QtCore import  pyqtSignal
        2)定义类中信号属性,“str”是参数
sendText = pyqtSignal(str)
      3)信号与槽绑定
myw.sendText.connect(myw1.deal_signal)
       4)发送信号

self.sendText.emit(str)

          在该项目功能需求中,需要获取MyMainFormlineEdit的内容,将其内容传递传递给MyMainForm1的deal_signal,并在deal_signal对MyMainForm1的文本设置,因此需要形参“str”,如果自定义信号不需要传递内容,则不需要添形参,如下代码即可

sendText = pyqtSignal()

最终实现

         

http://www.lryc.cn/news/247359.html

相关文章:

  • 高噪点灰度图目标粗定位CoraseLocation
  • Android:Google三方库之Firebase集成详细步骤(二)
  • java使用freemarker模板生成html,再生成pdf
  • 图解系列--Web服务器,Http首部
  • 直线(蓝桥杯)
  • Android:从源码看FragmentManager如何工作
  • LabVIEW通过编程将图形类控件的X轴显示为时间戳
  • Spring Boot进行单元测试,一个思路解决重启低效难题!
  • c/c++ header_only 头文件实现的关键点
  • Linux(CentOS7.5):通过docker安装redis
  • 唯创知音WT588F02B-8S语音芯片:灵活更换语音内容,降低开发成本与备货压力
  • git的创建以及使用
  • 面试笔记--Linux常用命令
  • 【小黑嵌入式系统第十课】μC/OS-III概况——实时操作系统的特点、基本概念(内核任务中断)、与硬件的关系实现
  • 在easyswoole 中,配置文件如何加载外部配置
  • 小程序微信支付API?以及参数有哪些
  • 【算法】一个简单的整数问题(树状数组、差分)
  • Android flutter项目 启动优化实战(二)利用 App Startup 优化项目和使用flutterboost中的问题解决
  • Java---权限修饰符、final、static
  • unity实时保存对象的位姿,重新运行程序时用最后保存的数据给物体赋值
  • 【Java Spring】Spring MVC基础
  • MES系统的功能清单
  • docker 安装elasticsearch集群
  • Spring Boot 3 + Spring Security 6 最新版本修改 Json 登录后 RememberMe 功能问题失效的解决方案
  • Java核心知识点整理大全21-笔记
  • Redis深入理解-主从架构下内核数据结构、主从同步以及主节点选举
  • java中BigDecimal的介绍及使用(二)
  • NX二次开发UF_MTX3_identity 函数介绍
  • 解决Hadoop DataNode ‘Incompatible clusterIDs‘报错
  • 计算机毕业设计|基于SpringBoot+MyBatis框架的电脑商城的设计与实现(系统概述与环境搭建)