From 64af9f63d76e45fb53bb2490eb32b20b60804b05 Mon Sep 17 00:00:00 2001 From: mostersl <1473450426@qq.com> Date: Wed, 17 Sep 2025 12:02:51 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=20SiLongPressPushButto?= =?UTF-8?q?n=20bug:=E7=AC=AC=E4=B8=80=E6=AC=A1=E7=82=B9=E5=87=BB=E9=AB=98?= =?UTF-8?q?=E4=BA=AE=E6=98=BE=E7=A4=BA=202.Qt.AlignVCenter=20=E5=8F=98?= =?UTF-8?q?=E6=9B=B4=20Qt.AlignmentFlag.AlignVCenter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- siui/components/widgets/button.py | 52 ++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/siui/components/widgets/button.py b/siui/components/widgets/button.py index ec563d7..c344c29 100644 --- a/siui/components/widgets/button.py +++ b/siui/components/widgets/button.py @@ -1,4 +1,4 @@ -from PyQt5.QtCore import QPoint, Qt, pyqtSignal +from PyQt5.QtCore import QPoint, Qt, pyqtSignal, QTimer from PyQt5.QtWidgets import QAbstractButton from siui.components.widgets.abstracts import ABCButton, ABCPushButton, ABCToggleButton, LongPressThread @@ -68,6 +68,56 @@ def setUseTransition(self, b: bool): """ self.use_transition = b +class SiLongPressPushButton(ABCPushButton): + """ + 长按点击事件的按钮,可以设置文字、图标或是兼有\n + 被绑定部件是一个 SiIconLabel,需要使用 attachment 方法来访问它 + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # 关闭自身触发点击的动画,以仅在按下超时后子再触发 + self.setFlashOnClicked(False) + + # 实例化按压线程,并绑定槽函数 + self.hold_timer = QTimer(self) + self.hold_timer.setSingleShot(True) + self.hold_timer.timeout.connect(self._process_changed_handler) + self.pressed.connect(self._start_timer) + self.released.connect(self._stop_timer) + + # 实例化文本标签 + self.label = SiIconLabel(self) + self.label.setSiliconWidgetFlag(Si.AdjustSizeOnTextChanged) + self.label.setFont(SiFont.tokenized(GlobalFont.S_NORMAL)) + self.label.setAlignment(Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignHCenter) + + # 设置偏移量,以保证在按钮明亮面显示 + self.setAttachmentShifting(0, -1) + + # 绑定到主体 + self.setAttachment(self.label) + + def _start_timer(self): + self.hold_timer.start(1) + + def _stop_timer(self): + self.hold_timer.stop() + self.reloadStyleSheet() + + def _process_changed_handler(self): + self.body_top.setColor(self.getColor(SiColor.BUTTON_FLASH)) + + def reloadStyleSheet(self): + super().reloadStyleSheet() + + # 设置文字颜色 + self.label.setTextColor(self.getColor(SiColor.TEXT_B)) + + self.body_top.setColor(self.getColor(SiColor.BUTTON_LONG_PRESS_PANEL)) + self.body_bottom.setColor(self.getColor(SiColor.BUTTON_LONG_PRESS_SHADOW)) + class SiLongPressButton(ABCPushButton): """ From 59281692d5327c37b53a3129849b6ad3b71706ea Mon Sep 17 00:00:00 2001 From: mostersl <1473450426@qq.com> Date: Wed, 17 Sep 2025 12:03:10 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20SiLongPressPushButton?= =?UTF-8?q?=20=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/page_homepage/page_homepage.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/Gallery for siui/components/page_homepage/page_homepage.py b/examples/Gallery for siui/components/page_homepage/page_homepage.py index d6423ac..5dbc3cc 100644 --- a/examples/Gallery for siui/components/page_homepage/page_homepage.py +++ b/examples/Gallery for siui/components/page_homepage/page_homepage.py @@ -2,7 +2,7 @@ from PyQt5.QtCore import Qt from PyQt5.QtGui import QFont -from siui.components import SiPixLabel +from siui.components import SiPixLabel, SiLongPressPushButton from siui.components.option_card import SiOptionCardLinear, SiOptionCardPlane from siui.components.page import SiPage from siui.components.slider import SiSliderH @@ -187,9 +187,14 @@ def __init__(self, *args, **kwargs): button_c.resize(128, 32) button_c.attachment().setText("Hold-to-Confirm") + button_d = SiLongPressPushButton(self) + button_d.resize(100, 32) + button_d.attachment().setText("Push Button") + option_card_button_container_h.addWidget(button_a) option_card_button_container_h.addWidget(button_b) option_card_button_container_h.addWidget(button_c) + option_card_button_container_h.addWidget(button_d) self.option_card_button.body().addWidget(option_card_button_container_h)