首页 > 解决方案 > 正确处理 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

标签: pythonpyqtpyqt4qaction

解决方案


推荐阅读