Pyqt5--Qobject
程序员文章站
2023-12-09 20:11:09
#!usr/bin/python# -*- coding: utf-8 -*-from PyQt5 import QtCorefrom PyQt5.QtCore import QObject, QEventfrom PyQt5.QtWidgets import *import sysapp = QApplication(sys.argv)class window(QWidget): def __init__(self): super().__init__() ....
#!usr/bin/python
# -*- coding: utf-8 -*-
from PyQt5 import QtCore
from PyQt5.QtCore import QObject, QEvent
from PyQt5.QtWidgets import *
import sys
app = QApplication(sys.argv)
class window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("第一个窗口")
self.resize(500, 500)
self.setup_ui()
def event(self, a0:QEvent) -> bool:
if a0.type()==QEvent.MouseButtonPress:
print("=================")
return super().event(a0)
def setup_ui(self):
lable = QLabel(self)
lable.move(100, 100)
lable.setText("Hello,world")
#可以根据不同的objectname来进行属性设置,相当于html中id,
lable.setObjectName("a")
# property也用来对属性进行限定设置
lable.setProperty("level","1")
lable1 = QLabel(self)
lable1.move(200, 200)
lable1.setText("你好,世界")
lable1.setObjectName("b")
lable2 = QLabel(self)
lable2.move(250, 250)
lable2.setText("你好,界")
lable2.setObjectName("b")
lable2.destroyed.connect(lambda :print("lable2被删除了"))
lable2.deleteLater()
self.test_Qobject()
print(lable.parent())
def test_Qobject(self):
objectt = QObject()
# 设置objctname
objectt.setObjectName("a")
print(objectt.objectName())
# 设置property
objectt.setProperty("name", "1")
objectt.setProperty("age", "2")
print(objectt.property("name"))
object1=QObject()
# 设置父元素
object1.setParent(objectt)
print("===========================")
# 获取所有子对象
print(objectt.children())
# 按条件查找子对象,参数1,子对象类型,参数2子对象名称 ,参数三,是否只查询直接子对象
objectt.findChild(QObject)
objectt.findChildren(QObject)
# 没有父控件的是顶层窗口,有父控件的子控件受父控件限制大小和生命周期
# 自带的两个信号监听,并且传递参数给接收函数
object1.destroyed.connect(lambda arg :print("{}对象被销毁释放了".format(arg.objectName)))
object1.objectNameChanged.connect(lambda arg:print("名字被改变了{}".format(arg)))
object1.setObjectName("aaa")
# 临时阻断
object1.blockSignals(True)
# 判断信号是否被阻断了
print(object1.signalsBlocked())
object1.setObjectName("ccc")
# 重新建立连接
object1.blockSignals(False)
object1.setObjectName("ddd")
# 一个信号可以有多个槽函数
object1.destroyed.connect(lambda arg: print("==============++++++++========="))
# 获取有多少个槽函数
print(object1.receivers(object1.objectNameChanged))
# 取消信号连接后,再更改名称就不会触发
object1.objectNameChanged.disconnect()
object1.setObjectName("bbbb")
# 判断是否继承自某个类型
print(object1.inherits("QObject"))
# 删除父控件,子控件也会被删除
del objectt
# 不能直接del object1因为它还有父控件引用它,不能被删除,使用deletelater
win = window()
# 加载qss文件
with open("Qobject.qss")as f:
app.setStyleSheet(f.read())
# 展示窗口
win.show()
# 进入消息循环
sys.exit(app.exec_())
参考链接:https://www.cnblogs.com/zach0812/category/1524140.html
本文地址:https://blog.csdn.net/lingxiyizhi_ljx/article/details/107445659