I have an application where users can add their own key sequences and open a file or folder when they hit their key sequences. I already did all the validations on the user's input and my app is almost complete until I encountered this problem.
Here's a code that will explain:
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]
method of disconnection to reload shortcuts:
for short in owner.shortcut_list:
owner.removeAction(short)
As you can see, I have two not so similar shortcuts "Q, R" and "Q, R, 1" where using the sequence "Q, R, 1" triggers the shortcut "Q, R" and does not read the next key stroke("Q, R, 1" does not work). I think the docs said that it will immediately trigger if it detected that is a valid sequence.
What do I do in this situation? A preferable solution is to make QAction
wait for a pause in key press then read whatever sequence it got. How do make both of these shortcuts to work?
EDIT
Code using 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]]
Disconnection Method(Does not work, produces an error in activated())
for short in owner.shortcut_list:
short[0].activatedAmbiguously.disconnect() #or short[0].activatedAmbiguously.disconnect(short[1])
short[0].deleteLater()
activated.disconnect() error:
disconnect failed bwtween activated and all of its connections
activated.disconnect(short[1]) error
function is not connected