正确处理QAU

2024-03-29 09:50:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个应用程序,用户可以添加他们自己的键序列,并打开一个文件或文件夹时,他们点击他们的键序列。我已经对用户的输入做了所有的验证,我的应用程序几乎完成了,直到我遇到这个问题。你知道吗

下面的代码将解释:

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]]

断开方法(不起作用,在activated()中产生错误)

 for short in owner.shortcut_list:
    short[0].activatedAmbiguously.disconnect() #or short[0].activatedAmbiguously.disconnect(short[1])
    short[0].deleteLater()

你知道吗激活。断开()错误:

disconnect failed bwtween activated and all of its connections

你知道吗激活。断开(short[1])错误

function is not connected

Tags: topathlambdainfordata错误序列