在 PySide6 中,QShortcut
类提供了一种便捷的方式来为应用程序设置键盘快捷键,从而提升用户交互效率。通过将快捷键绑定到特定的动作或函数,用户可以快速执行常用操作,无需依赖菜单或按钮。
QShortcut 的基本用法
下面的示例展示了如何为一个简单的窗口设置快捷键,当按下Ctrl + P时打印消息,当按下ESC时退出程序。
import sys
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtGui import QShortcut
from PySide6.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QShortcut 示例")
self.setGeometry(100, 100, 400, 300)
# 创建快捷键,绑定 Ctrl + P
sc1 = QShortcut(self)
sc1.setKey(Qt.CTRL | Qt.Key_P)
sc1.activated.connect(self.ctrl_p)
# 绑定 ESC
sc2 = QShortcut("ESC",self)
sc2.activated.connect(QApplication.exit)
def ctrl_p(self):
print("Ctrl + P 被按下")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
QShortcut 常用初始化方法
QShortcut 有多种初始化方式:
from PySide6.QtWidgets import QShortcut, QWidget
from PySide6.QtGui import QKeySequence
from PySide6.QtCore import Qt
# 方式1:使用字符串设置快捷键
shortcut = QShortcut(QKeySequence("Ctrl+S"), parent_widget)
# 方式2:使用标准键
shortcut = QShortcut(QKeySequence.Save, parent_widget)
# 方式3:使用 Qt 枚举组合
shortcut = QShortcut(QKeySequence(Qt.CTRL | Qt.Key.Key_S), parent_widget)
# 方式4:先创建后设置
shortcut = QShortcut(parent_widget)
shortcut.setKey(QKeySequence("Ctrl+S"))
也可以直接使用字符串:
shortcut = QShortcut("Ctrl+S", parent_widget)
activated 信号
QShortcut 的activated信号在快捷键被按下时触发,可以连接到任何可调用的函数或方法。
shortcut.activated.connect(function_name)
activatedAmbiguously 信号
当一个快捷键按键序列被多个 QShortcut 对象同时注册时,按下该快捷键会触发所有相关快捷键的 activatedAmbiguously 信号,而不会触发 activated 信号。
sc1 = QShortcut("ESC",self)
sc1.activated.connect(QApplication.exit)
sc1.activatedAmbiguously.connect(lambda:print("按键冲突"))
sc2 = QShortcut("ESC",self)
sc2.activated.connect(QApplication.exit)
Qt.Key 枚举常量
Qt.Key 是 Qt 框架中用于表示键盘按键的枚举类型。
| 枚举常量 | 简短描述 |
|---|---|
Qt.Key_Escape | Esc键,通常用于取消或退出 |
Qt.Key_Tab | Tab键,用于焦点切换 |
Qt.Key_Backtab | Shift+Tab组合键 |
Qt.Key_Backspace | 退格键 |
Qt.Key_Return | 主键盘区的回车键 |
Qt.Key_Enter | 数字键盘区的回车键 |
Qt.Key_Insert | 插入键 |
Qt.Key_Delete | 删除键 |
Qt.Key_Pause | 暂停/Break键 |
Qt.Key_Print | 打印屏幕键 |
Qt.Key_SysReq | 系统请求键 |
Qt.Key_Clear | 清除键 |
Qt.Key_Home | Home键 |
Qt.Key_End | End键 |
Qt.Key_Left | 左箭头键 |
Qt.Key_Up | 上箭头键 |
Qt.Key_Right | 右箭头键 |
Qt.Key_Down | 下箭头键 |
Qt.Key_PageUp | 上一页键 |
Qt.Key_PageDown | 下一页键 |
Qt.Key_Shift | Shift键 |
Qt.Key_Control | Ctrl键 |
Qt.Key_Meta | Meta键 |
Qt.Key_Alt | Alt键 |
Qt.Key_AltGr | AltGr键 |
Qt.Key_CapsLock | 大写锁定键 |
Qt.Key_NumLock | 数字锁定键 |
Qt.Key_ScrollLock | 滚动锁定键 |
Qt.Key_F1 | 功能键F1 |
Qt.Key_F2 | 功能键F2 |
Qt.Key_F3 | 功能键F3 |
Qt.Key_F4 | 功能键F4 |
Qt.Key_F5 | 功能键F5 |
Qt.Key_F6 | 功能键F6 |
Qt.Key_F7 | 功能键F7 |
Qt.Key_F8 | 功能键F8 |
Qt.Key_F9 | 功能键F9 |
Qt.Key_F10 | 功能键F10 |
Qt.Key_F11 | 功能键F11 |
Qt.Key_F12 | 功能键F12 |
Qt.Key_F13 | 功能键F13 |
Qt.Key_F14 | 功能键F14 |
Qt.Key_F15 | 功能键F15 |
Qt.Key_F16 | 功能键F16 |
Qt.Key_F17 | 功能键F17 |
Qt.Key_F18 | 功能键F18 |
Qt.Key_F19 | 功能键F19 |
Qt.Key_F20 | 功能键F20 |
Qt.Key_F21 | 功能键F21 |
Qt.Key_F22 | 功能键F22 |
Qt.Key_F23 | 功能键F23 |
Qt.Key_F24 | 功能键F24 |
Qt.Key_F25 | 功能键F25 |
Qt.Key_F26 | 功能键F26 |
Qt.Key_F27 | 功能键F27 |
Qt.Key_F28 | 功能键F28 |
Qt.Key_F29 | 功能键F29 |
Qt.Key_F30 | 功能键F30 |
Qt.Key_F31 | 功能键F31 |
Qt.Key_F32 | 功能键F32 |
Qt.Key_F33 | 功能键F33 |
Qt.Key_F34 | 功能键F34 |
Qt.Key_F35 | 功能键F35 |
Qt.Key_Space | 空格键 |
Qt.Key_A | 字母A |
Qt.Key_B | 字母B |
Qt.Key_C | 字母C |
Qt.Key_D | 字母D |
Qt.Key_E | 字母E |
Qt.Key_F | 字母F |
Qt.Key_G | 字母G |
Qt.Key_H | 字母H |
Qt.Key_I | 字母I |
Qt.Key_J | 字母J |
Qt.Key_K | 字母K |
Qt.Key_L | 字母L |
Qt.Key_M | 字母M |
Qt.Key_N | 字母N |
Qt.Key_O | 字母O |
Qt.Key_P | 字母P |
Qt.Key_Q | 字母Q |
Qt.Key_R | 字母R |
Qt.Key_S | 字母S |
Qt.Key_T | 字母T |
Qt.Key_U | 字母U |
Qt.Key_V | 字母V |
Qt.Key_W | 字母W |
Qt.Key_X | 字母X |
Qt.Key_Y | 字母Y |
Qt.Key_Z | 字母Z |
Qt.Key_0 | 数字0 |
Qt.Key_1 | 数字1 |
Qt.Key_2 | 数字2 |
Qt.Key_3 | 数字3 |
Qt.Key_4 | 数字4 |
Qt.Key_5 | 数字5 |
Qt.Key_6 | 数字6 |
Qt.Key_7 | 数字7 |
Qt.Key_8 | 数字8 |
Qt.Key_9 | 数字9 |
Qt.Key_VolumeDown | 音量减 |
Qt.Key_VolumeMute | 静音 |
Qt.Key_VolumeUp | 音量加 |
Qt.Key_MediaPlay | 播放 |
Qt.Key_MediaStop | 停止 |
Qt.Key_MediaPrevious | 上一曲 |
Qt.Key_MediaNext | 下一曲 |
Qt.Key_MediaRecord | 录音 |
Qt.Key_MediaPause | 暂停 |
Qt.Key_MediaTogglePlayPause | 播放/暂停切换 |
Qt.Key_Help | 帮助 |
Qt.Key_Menu | 菜单键 |
Qt.Key_Undo | 撤销 |
Qt.Key_Redo | 重做 |
Qt.Key_Find | 查找 |
Qt.Key_New | 新建 |
Qt.Key_Open | 打开 |
Qt.Key_Save | 保存 |
Qt.Key_Close | 关闭 |
Qt.Key_Copy | 复制 |
Qt.Key_Cut | 剪切 |
Qt.Key_Paste | 粘贴 |
Qt.Key_ZoomIn | 放大 |
Qt.Key_ZoomOut | 缩小 |
Qt.Key_Sleep | 睡眠 |
Qt.Key_PowerDown | 关机 |
Qt.Key_Hibernate | 休眠 |
Qt.Key_WakeUp | 唤醒 |
Qt.Key_Eject | 弹出 |
Qt.Key_unknown | 未知按键 |
QKeySequence 常量
QKeySequence 提供了一系列预定义的标准快捷键常量,用于表示跨平台通用的键盘快捷键。
| 常量 | Windows/Linux | macOS | 说明 |
|---|---|---|---|
QKeySequence.Copy |
Ctrl+C | Cmd+C | 复制 |
QKeySequence.Cut |
Ctrl+X | Cmd+X | 剪切 |
QKeySequence.Paste |
Ctrl+V | Cmd+V | 粘贴 |
QKeySequence.Undo |
Ctrl+Z | Cmd+Z | 撤销 |
QKeySequence.Redo |
Ctrl+Y | Cmd+Shift+Z | 重做 |
QKeySequence.Save |
Ctrl+S | Cmd+S | 保存 |
QKeySequence.SaveAs |
Ctrl+Shift+S | Cmd+Shift+S | 另存为 |
QKeySequence.Open |
Ctrl+O | Cmd+O | 打开 |
QKeySequence.New |
Ctrl+N | Cmd+N | 新建 |
QKeySequence.Close |
Ctrl+W | Cmd+W | 关闭 |
QKeySequence.Quit |
Ctrl+Q | Cmd+Q | 退出 |
QKeySequence.Print |
Ctrl+P | Cmd+P | 打印 |
QKeySequence.Delete |
Del | Del | 删除 |
QKeySequence.Find |
Ctrl+F | Cmd+F | 查找 |
QKeySequence.FindNext |
F3 | Cmd+G | 查找下一个 |
QKeySequence.FindPrevious |
Shift+F3 | Cmd+Shift+G | 查找上一个 |
QKeySequence.Replace |
Ctrl+H | Cmd+H | 替换 |
QKeySequence.SelectAll |
Ctrl+A | Cmd+A | 全选 |
QKeySequence.Deselect |
Ctrl+Shift+A | Cmd+Shift+A | 取消全选 |
QKeySequence.DeleteEndOfWord |
Ctrl+Del | Alt+Del | 删除到词尾 |
QKeySequence.DeleteStartOfWord |
Ctrl+Backspace | Alt+Backspace | 删除到词首 |
QKeySequence.MoveToNextWord |
Ctrl+Right | Alt+Right | 移到下一个词 |
QKeySequence.MoveToPreviousWord |
Ctrl+Left | Alt+Left | 移到上一个词 |
QKeySequence.MoveToNextLine |
Down | Down | 移到下一行 |
QKeySequence.MoveToPreviousLine |
Up | Up | 移到上一行 |
QKeySequence.MoveToNextChar |
Right | Right | 移到下一个字符 |
QKeySequence.MoveToPreviousChar |
Left | Left | 移到上一个字符 |
QKeySequence.MoveToStartOfLine |
Home | Cmd+Left | 移到行首 |
QKeySequence.MoveToEndOfLine |
End | Cmd+Right | 移到行尾 |
QKeySequence.MoveToStartOfBlock |
Ctrl+Up | Cmd+Up | 移到块首 |
QKeySequence.MoveToEndOfBlock |
Ctrl+Down | Cmd+Down | 移到块尾 |
QKeySequence.MoveToStartOfDocument |
Ctrl+Home | Cmd+Up | 移到文档开头 |
QKeySequence.MoveToEndOfDocument |
Ctrl+End | Cmd+Down | 移到文档结尾 |
QKeySequence.SelectNextWord |
Ctrl+Shift+Right | Alt+Shift+Right | 选中下一个词 |
QKeySequence.SelectPreviousWord |
Ctrl+Shift+Left | Alt+Shift+Left | 选中上一个词 |
QKeySequence.SelectNextLine |
Shift+Down | Shift+Down | 选中下一行 |
QKeySequence.SelectPreviousLine |
Shift+Up | Shift+Up | 选中上一行 |
QKeySequence.SelectNextChar |
Shift+Right | Shift+Right | 选中下一个字符 |
QKeySequence.SelectPreviousChar |
Shift+Left | Shift+Left | 选中上一个字符 |
QKeySequence.SelectStartOfLine |
Shift+Home | Cmd+Shift+Left | 选中到行首 |
QKeySequence.SelectEndOfLine |
Shift+End | Cmd+Shift+Right | 选中到行尾 |
QKeySequence.SelectStartOfBlock |
Ctrl+Shift+Up | Cmd+Shift+Up | 选中到块首 |
QKeySequence.SelectEndOfBlock |
Ctrl+Shift+Down | Cmd+Shift+Down | 选中到块尾 |
QKeySequence.SelectStartOfDocument |
Ctrl+Shift+Home | Cmd+Shift+Up | 选中到文档开头 |
QKeySequence.SelectEndOfDocument |
Ctrl+Shift+End | Cmd+Shift+Down | 选中到文档结尾 |
QKeySequence.DeleteCompleteLine |
Ctrl+Shift+Del | Cmd+Shift+Del | 删除整行 |
QKeySequence.InsertParagraphSeparator |
Enter | Enter | 插入段落分隔符 |
QKeySequence.InsertLineSeparator |
Shift+Enter | Shift+Enter | 插入换行符 |
QKeySequence.Backspace |
Backspace | Backspace | 退格删除 |
QKeySequence.Help |
F1 | Cmd+? | 帮助 |
QKeySequence.WhatsThis |
Shift+F1 | Shift+F1 | 这是什么 |
QKeySequence.Preferences |
Ctrl+, | Cmd+, | 偏好设置 |
QKeySequence.ZoomIn |
Ctrl++ | Cmd++ | 放大 |
QKeySequence.ZoomOut |
Ctrl+- | Cmd+- | 缩小 |
QKeySequence.FullScreen |
F11 | Cmd+Ctrl+F | 全屏 |
QKeySequence.Refresh |
F5 | Cmd+R | 刷新 |
QKeySequence.Forward |
Alt+Right | Cmd+] | 前进 |
QKeySequence.Back |
Alt+Left | Cmd+[ | 后退 |