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的对象列表

|------------|------------|
| 对象名 | 类型 |
| lineEdit | LlineEdit |
| pushButton | QPushButto |

untitled1.ui的对象列表

|-------|--------|
| 对象名 | 类型 |
| label | QLabel |

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

python 复制代码
# 导入sys模块
import sys
# PyQt6.QtWidgets模块中导入QApplication, QWidget
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtCore import QObject

import untitled
import untitled1

class 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)
    # 实例化MyMainForm
    myw = MyMainForm()
    myw.show()
    myw1 = MyMainForm1()
    myw1.show()
    myw.sendText.connect(myw1.deal_signal)

    # 启动应用程序的事件循环并等待用户交互,直到应用程序关闭。
    sys.exit(app.exec())

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

python 复制代码
self.move(1200,320)

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

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

python 复制代码
from untitled import Ui_Form
from untitled1 import Ui_Form

正确书写应该是这样

python 复制代码
import untitled
import untitled1
class MyMainForm(QWidget, untitled.Ui_Form):
class MyMainForm1(QWidget, untitled1.Ui_Form):

问题

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

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

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

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

自定义信号

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

python 复制代码
 myw.sendText.connect(myw1.deal_signal)
修改代码如下
python 复制代码
# 导入sys模块
import sys
# PyQt6.QtWidgets模块中导入QApplication, QWidget
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtCore import QObject, pyqtSignal


import untitled
import untitled1

class 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)
    # 实例化MyMainForm
    myw = MyMainForm()
    myw.show()
    myw1 = MyMainForm1()
    myw1.show()
    myw.sendText.connect(myw1.deal_signal)

    # 启动应用程序的事件循环并等待用户交互,直到应用程序关闭。
    sys.exit(app.exec())
自定义信号过程
1)导入 pyqtSignal类
python 复制代码
from PyQt6.QtCore import  pyqtSignal
2)定义类中信号属性,"str"是参数
python 复制代码
sendText = pyqtSignal(str)
3)信号与槽绑定
python 复制代码
myw.sendText.connect(myw1.deal_signal)
4)发送信号
python 复制代码
self.sendText.emit(str)

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

python 复制代码
sendText = pyqtSignal()

最终实现

相关推荐
云空1 天前
《PyQt6-3D应用开发技术文档》
3d·pyqt
sword devil9001 天前
PYQT实战:无刷电机模拟(只是模拟,没有写接口接收外部数据)
pyqt
sword devil9004 天前
PYQT实战:智能家居中控
python·智能家居·pyqt
OICQQ676580089 天前
创建一个基于YOLOv8+PyQt界面的驾驶员疲劳驾驶检测系统 实现对驾驶员疲劳状态的打哈欠检测,头部下垂 疲劳眼睛检测识别
yolo·pyqt·疲劳驾驶·检测识别·驾驶员检测·打哈欠检测·眼睛疲劳
小灰灰搞电子18 天前
Qt PyQt与PySide技术-C++库的Python绑定
c++·qt·pyqt
越甲八千19 天前
pyqt 简单条码系统
数据库·microsoft·pyqt
重生之我在厦门做测试20 天前
基于pyqt开发串口和对应映射值(path)的显示工具
pyqt
hvinsion23 天前
【开源解析】基于Python+Qt打造智能应用时长统计工具 - 你的数字生活分析师
python·pyqt·开源软件·应用时长统计
毕设做完了吗?1 个月前
基于YOLOv8+Deepface的人脸检测与识别系统
python·yolo·毕业设计·pyqt
懷淰メ1 个月前
python3GUI--基于PyQt5+DeepSort+YOLOv8智能人员入侵检测系统(详细图文介绍)
开发语言·深度学习·yolo·目标检测·pyqt·课程设计·deepsort