Properly handling of QAction shortcut

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Properly handling of QAction shortcut



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?


QAction



EDIT



Code using QShortcut


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





QShortcut.activatedAmbiguously
– ekhumoro
Aug 5 at 0:17





I have tried to use QShortcut first but unfortunately I can't seem to disconnect them to reload all shortcuts when the user changes a shortcut sequence
– Juan Carlos Asuncion
Aug 6 at 1:21


QShortcut





Also, activatedAmbiguously does not trigger, maybe I am doing something wrong? shortcut.activatedAmbiguously.connect(lambda p=data[0]: func_to_open(p))
– Juan Carlos Asuncion
Aug 6 at 1:30


activatedAmbiguously


shortcut.activatedAmbiguously.connect(lambda p=data[0]: func_to_open(p))









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS