python - 正确处理 QAction 快捷方式
问题描述
我有一个应用程序,用户可以在其中添加自己的按键序列,并在按下按键序列时打开文件或文件夹。我已经对用户的输入进行了所有验证,并且我的应用程序几乎完成,直到我遇到这个问题。
这是一个将解释的代码:
result = [["path\\to\\foler", "Q, R"], ["path\\to\\file.ext", "Q, R, 1"]]
for data in result:
if os.path.exists(data[0]):
shortcut = QAction(owner)
shortcut.setShortcut(QKeySequence(data[1]))
shortcut.triggered.connect(lambda checked, p=data[0]: func_to_open(p))
owner.addAction(shortcut)
owner.shortcut_list += [shortcut]
断开连接重新加载快捷方式的方法:
for short in owner.shortcut_list:
owner.removeAction(short)
如您所见,我有两个不太相似的快捷方式“Q,R”和“Q,R,1”,其中使用序列“Q,R,1”会触发快捷方式“Q,R”并且不会读取下一个击键(“Q,R,1”不起作用)。我认为文档说如果检测到这是一个有效序列,它将立即触发。
在这种情况下我该怎么办?一个更好的解决方案是QAction
等待按键暂停,然后读取它得到的任何序列。如何使这两个快捷方式起作用?
编辑
代码使用QShortcut
result = [["path\\to\\foler", "Q, R"], ["path\\to\\file.ext", "Q, R, 1"]]
for data in result:
if os.path.exists(data[0]):
shortcut = QShortcut(QKeySequence(book[1]), owner)
lambda_func = lambda w=path: func_to_open(w)
#Does Not Trigger, What is wrong?(Nothing happens)
shortcut.activatedAmbiguously.connect(lambda_func)
owner.shortcut_list += [[shortcut, lambda_func]]
断开连接方法(不起作用,在激活()中产生错误)
for short in owner.shortcut_list:
short[0].activatedAmbiguously.disconnect() #or short[0].activatedAmbiguously.disconnect(short[1])
short[0].deleteLater()
激活.disconnect() 错误:
disconnect failed bwtween activated and all of its connections
激活.disconnect(short[1]) 错误
function is not connected
解决方案
推荐阅读
- java - Java 文件导入
- function - Kotlin:将对象列表转换为对象属性映射
- ios - Swift iOS 在表格视图中我可以在添加行后使用 reloadData 吗?
- python - 熊猫从具有名称列表的列中获取最常用的名称
- c - C:为什么没有得到我输出的最后一行?
- javascript - 如何在本机反应中呈现html表格?
- karate - 空手道场景创建以测试 JSON 格式返回的错误消息
- ios - 缩放或移动时谷歌地图滞后
- javascript - 我的 Firebase 云函数因 Object.value is not a function 错误而失败?
- vb.net - 当我运行程序时,出现错误(cmd.executenonquery)