Add 03-02-01-03

This commit is contained in:
muzing 2022-06-18 23:31:05 +08:00
parent 575404a943
commit a05a451355

View File

@ -0,0 +1,50 @@
import sys
from PySide6 import QtWidgets
"""
QAbstractButton 自动重复
按钮可以设置自动重复按下不放时反复发射 pressed() released() clicked() 信号
推荐阅读https://doc.qt.io/qt-6/qabstractbutton.html#autoRepeat-prop
.setAutoRepeat(bool) 设置是否开启自动重复默认关闭
.setAutoRepeatDelay(int) 设置触发自动重复的延时即按住多长时间后才开始自动重复单位为毫秒
.setAutoRepeatInterval(int) 设置自动重复的时间间隔单位为毫秒
.autoRepeat() -> bool 获取自动重复状态
.autoRepeatDelay() -> int 获取自动重复延时单位毫秒
.autoRepeatInterval() -> int 获取自动重复时间间隔单位毫秒
"""
class MyWidget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle("QAbstractButton-自动重复")
self.resize(800, 600)
self.setup_ui()
def setup_ui(self) -> None:
"""设置界面"""
# 由于QAbstractButton是抽象类不能被实例化使用QPushButton演示
button = QtWidgets.QPushButton("按钮自动重复", self)
button.move(350, 200)
button.clicked.connect(lambda: print("按钮被点击了")) # type: ignore
# 设置自动重复
button.setAutoRepeat(True) # 启用自动重复
button.setAutoRepeatDelay(1000) # 按住按钮1秒才开始自动重复
button.setAutoRepeatInterval(300) # 每300毫秒重复一次
# 当按住按钮不放超过1秒后按钮自动反复发射clicked等信号松开按钮后停止
print(button.autoRepeat())
print(button.autoRepeatDelay())
print(button.autoRepeatInterval())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyWidget()
window.show()
sys.exit(app.exec())