目录
setParent(self, parent: QWidget):
setProperty(self, name, value):
blockSignals(self, b: bool) -> bool
destroyed(self, p_object, QObject=None, *args, **kwargs)
receivers(self, signal: PYQT_SIGNAL) -> int
installEventFilter(self, a0: QObject)
eventFilter(self, a0: QObject, a1: QEvent)
startTimer(self, interval: int, timerType: Qt.TimerType = Qt.CoarseTimer)
eventFilter(self, a0: QObject, a1: QEvent)
tr(self, sourceText, disambiguation, p_str=None, *args, **kwargs):
简介:
QObject
setParent(self, parent: QWidget):
设置父节点,如果不存在父节点,那么当前节点则为主窗口。当父节点被销毁的时候,所有子节点也会被销毁。
parent(self):
返回父节点
children(self):
返回所有子节点 List[QObject]
findChildren(self, *__args):
查询子节点, 可以根据类型, objectname,多种方式查询。
setObjectName(self, name):
设置对象名,类似唯一id。
qss : QLabel#notice { font-size: 20px; color: gray; border: 1px solid gray; border-radius: 8px; }
label2.setObjectName("notice")
setProperty(self, name, value):
设置属性名和值.
dynamicPropertyNames(self):
返回所有属性名
property(self, name):
根据属性名,返回属性值
代码
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.setWindowTitle("title")
self.resize(500, 300)
self.__initWidget()
def __initWidget(self):
print(f"设置父对象")
self.setParent(None)
print(f"父节点:{self.parent()}")
print(f"获取子对象:{self.children()}")
print(f"查找子对象{self.findChildren(QWidget)}")
print(f"设置对象ID")
self.setObjectName("window")
print(f'对象ID:{self.objectName()}')
print(f"设置属性名称")
self.setProperty("demaxiya", "德玛西亚")
self.setProperty("aiouniya", "艾欧尼亚")
print(f"获取全部属性名称")
for key in self.dynamicPropertyNames():
print(f"属性值:{self.property(key)}")
blockSignals(self, b: bool) -> bool
临时阻断链接 True, 重启连接 False
权限管理肯定很有用。
disconnect(self, a0=None):
断开信号连接
obj1.objectNameChanged.disconnect()
destroyed(self, p_object, QObject=None, *args, **kwargs)
对象销毁
object2.destroyed.connect(lambda: print("对象被摧毁"))
receivers(self, signal: PYQT_SIGNAL) -> int
返回信号连接数量
object1.receivers(object1.objectNameChanged))
代码
def test_blockSignals(self):
object1 = QObject()
object2 = QObject()
object1.setParent(object2)
object2.destroyed.connect(lambda: print("对象被摧毁"))
object1.objectNameChanged.connect(lambda name: print("对象名称被改变: " + name))
object1.setObjectName("xxx")
object1.blockSignals(True) # 临时阻断连接
object1.setObjectName("1")
print("连接数量:" + str(object1.receivers(object1.objectNameChanged))) # 查看有多少个槽
object1.blockSignals(False) # 再次开启连接
object1.objectNameChanged.connect(lambda name: print("第二个连接:" + name))
print("连接数量:" + str(object1.receivers(object1.objectNameChanged))) # 查看有多少个槽连接这个信号
object1.setObjectName("2")
del object2
deleteLater(self)
deleteLater 会在当前对象的所有事件处理完成后再删除对象
delete demo 会立即删除对象
installEventFilter(self, a0: QObject)
安装拦截事件对象
eventFilter(self, a0: QObject, a1: QEvent)
事件拦截器
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.setWindowTitle("title")
self.button = QPushButton("button", self)
self.resize(500, 300)
self.installEventFilter(self)
self.__initWidget()
def __initWidget(self):
print(f"设置父对象")
self.setParent(None)
print(f"父节点:{self.parent()}")
print(f"获取子对象:{self.children()}")
print(f"查找子对象{self.findChildren(QWidget)}")
print(f"设置对象ID")
self.setObjectName("window")
print(f'对象ID:{self.objectName()}')
print(f"设置属性名称")
self.setProperty("demaxiya", "德玛西亚")
self.setProperty("aiouniya", "艾欧尼亚")
print(f"获取全部属性名称")
for key in self.dynamicPropertyNames():
print(f"属性值:{self.property(key)}")
def eventFilter(self, a, e):
if a == self:
# 按键
if e.type() == QEvent.KeyPress:
if e.key() == Qt.Key_Tab:
print("按下了tab键")
# 只要在 eventFilter 中返回 True,就能防止该事件被子控件得到
return True
if e.type() == QEvent.MouseButtonDblClick:
print("鼠标双击事件")
if e.type() == QEvent.MouseMove:
print("鼠标移到")
if e.type() == QEvent.MouseButtonPress:
print("鼠标单击事件")
if e.type() == QEvent.Close:
print("关闭事件")
return QObject.eventFilter(self, a, e) # 交由其他控件处理
isWidgetType(self)
是否是widget
isWindowType(self):
是否是window
startTimer(self, interval: int, timerType: Qt.TimerType = Qt.CoarseTimer)
开启定时器 :interval 毫秒
-
QtTimeType:控制时间的精度
-
QtPreciseTimer:精确计时器尽可能到毫秒精度
QtCoarseTimer:粗计时器试图保持精度在预期间隔的5%以内。
QtVeryCoarseTimer:非常粗糙的计时器,只能到秒。
-
-
timer_id:定时器唯一的标识
eventFilter(self, a0: QObject, a1: QEvent)
定时器事件:
killTimer(self, id: int)
结束定时器
def timerEvent(self, evt):
print(evt, "1")
def test_EventFilter(self):
app = QApplication(sys.argv)
w = Window()
print(f"是否是widget {w.isWidgetType()}")
# 毫秒1000 = 1秒
id = w.startTimer(1000)
# w.killTimer(id)
w.show()
app.exec_()
sender(self) -> QObject
信号发送者
if self.sender() == self.button:
print("点击了Buttonn ")
tr(self, sourceText, disambiguation, p_str=None, *args, **kwargs):
网上说的比较少,解决国际化的。