A few questions regarding shortcuts
-
I'm learning about shortcuts to be used in Maya using PySide2.
I've come to understand there's a few ways they can be created, but please correct me if I'm wrong.
The three ways are: QShortcut, QEvent and QAction.
It seems QEvent is the only way to make a shortcut with Press/Release functionality.As I created a QShortcut by:
shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.ALT + QtCore.Qt.Key_F), _getMainMayaWindow()) shortcut.setContext(QtCore.Qt.ApplicationShortcut) shortcut.activated.connect(partial(createPolyCube, shortcut))
then I can get all QShortcut:s under Maya by:
for shortcut in mayaMainWindow.findChildren(QShortcut): print(shortcut.key().toString())
But is there a way I can get the command that is connected to the shortcut? In this case "createPolyCube". And is it possible to give the QShortcut object a name to easily identify it? I can't seem to find anything about this.
-
-
Hi,
For the object name, you can use the objectName property.
As for the connected slots, why do you need that information ?
-
@SGaist Ah Thanks a lot!
I can of course give it the name of the command, but I was just curious as I'm testing and adding commands to QShortcut objects and then iterating through them I wanted to see what information is there should I ever need it to act upon. -
The thing is that the usual design is for the signal emitter to just not care about what is connected to it. That's outside of is responsibility.
-
So I've put together this little code:
from maya import OpenMayaUI as omui from PySide2 import QtWidgets, QtGui, QtCore from shiboken2 import wrapInstance mayaMainWindowPtr = omui.MQtUtil.mainWindow() mayaMainWindow = wrapInstance(int(mayaMainWindowPtr), QtWidgets.QWidget) key_filter = KeyFilter() mayaMainWindow.installEventFilter(key_filter) class KeyFilter(QtCore.QObject): def eventFilter(self, obj, event): if event.type() == QtCore.QEvent.KeyPress and not event.isAutoRepeat() and event.key() == QtCore.Qt.Key_section and not event.modifiers() == QtCore.Qt.AltModifier: # only section key print('open') elif event.type() == QtCore.QEvent.KeyRelease and not event.isAutoRepeat() and event.key() == QtCore.Qt.Key_section and not event.modifiers() == QtCore.Qt.AltModifier: # only section key print('close') return False
This creates a QObject with an eventFilter which is installed in the Maya UI.
Is there a way to loop through the children of mayaMainWindow and find this KeyFilter object so that I can remove all shortcuts in one fell swoop?
Or is it just better to collect all the objects in a list upon creation and loop through that by:mayaMainWindow.removeEventFilter(key_filter)
-
@Geuse said in A few questions regarding shortcuts:
Is there a way to loop through the children of mayaMainWindow and find this KeyFilter object so that I can remove all shortcuts in one fell swoop?
I don't understand how your
KeyFilter
is related to your shortcuts.
KeyPressEvent handling andQShortcut
are two different approaches...In Qt 6.5 there is an extra shortcut event class, but even there it's mentioned that you probably better use
QShortcut
directly.
(and since you are using PySide2 you can't use it anyway... why this old version?!)What's your idea behind all this? What do you want to achieve with your shortcuts?
-
@Pl45m4 Oh, no, I don't use QShortcut with the eventFilter.
Only reason I'm looking into eventFilter is to have press/release functionality. One command on press, another when released. Only way I found that is to use an eventFilter like this.
Ideally I also want support for holding a key, for continuing re-execution of said command. -
@Geuse said in A few questions regarding shortcuts:
One command on press, another when released
You could simply override https://doc.qt.io/qt-6/qwidget.html#mousePressEvent and https://doc.qt.io/qt-6/qwidget.html#mouseReleaseEvent
And https://doc.qt.io/qt-6/qinputevent.html#modifiers if you want to know whether some key is pressed. -