Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. A few questions regarding shortcuts
Forum Updated to NodeBB v4.3 + New Features

A few questions regarding shortcuts

Scheduled Pinned Locked Moved Unsolved Qt for Python
12 Posts 4 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Geuse
    wrote on last edited by Geuse
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS SGaist moved this topic from General and Desktop on
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      For the object name, you can use the objectName property.

      As for the connected slots, why do you need that information ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      G 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        For the object name, you can use the objectName property.

        As for the connected slots, why do you need that information ?

        G Offline
        G Offline
        Geuse
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          G 1 Reply Last reply
          0
          • SGaistS SGaist

            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.

            G Offline
            G Offline
            Geuse
            wrote on last edited by
            #5

            @SGaist Gotcha, thanks so much!

            1 Reply Last reply
            0
            • G Offline
              G Offline
              Geuse
              wrote on last edited by
              #6

              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)
              
              Pl45m4P 1 Reply Last reply
              0
              • G Geuse

                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)
                
                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #7

                @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 and QShortcut 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?!)

                • https://doc.qt.io/qt-6/qshortcutevent.html#details

                What's your idea behind all this? What do you want to achieve with your shortcuts?


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                G 1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @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 and QShortcut 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?!)

                  • https://doc.qt.io/qt-6/qshortcutevent.html#details

                  What's your idea behind all this? What do you want to achieve with your shortcuts?

                  G Offline
                  G Offline
                  Geuse
                  wrote on last edited by Geuse
                  #8

                  @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.

                  jsulmJ 1 Reply Last reply
                  0
                  • G Geuse

                    @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.

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    G 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @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.

                      G Offline
                      G Offline
                      Geuse
                      wrote on last edited by Geuse
                      #10

                      @jsulm Hi, how does that work?
                      I'm confused how mousePressEvent help me. I just want tohave keyboard shortcuts that works on press and release of said keys.

                      jsulmJ 1 Reply Last reply
                      0
                      • G Geuse

                        @jsulm Hi, how does that work?
                        I'm confused how mousePressEvent help me. I just want tohave keyboard shortcuts that works on press and release of said keys.

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Geuse In tha tcase https://doc.qt.io/qt-6/qwidget.html#keyPressEvent and https://doc.qt.io/qt-6/qwidget.html#keyReleaseEvent

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        G 1 Reply Last reply
                        2
                        • jsulmJ jsulm

                          @Geuse In tha tcase https://doc.qt.io/qt-6/qwidget.html#keyPressEvent and https://doc.qt.io/qt-6/qwidget.html#keyReleaseEvent

                          G Offline
                          G Offline
                          Geuse
                          wrote on last edited by
                          #12

                          @jsulm Thank you. I will look into that!

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved