正确处理QAction快捷方式

时间:2018-08-03 01:55:19

标签: python pyqt pyqt4 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]]

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

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

activated.disconnect()错误:

disconnect failed bwtween activated and all of its connections

activated.disconnect(short [1])错误

function is not connected

0 个答案:

没有答案