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. PyQt5 - How to stop displaying shortcut in QAction?
Forum Updated to NodeBB v4.3 + New Features

PyQt5 - How to stop displaying shortcut in QAction?

Scheduled Pinned Locked Moved Unsolved Qt for Python
11 Posts 4 Posters 916 Views 1 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.
  • PatitotectiveP Offline
    PatitotectiveP Offline
    Patitotective
    wrote on last edited by Patitotective
    #1

    I'm trying to stop displaying the shortcut in the QAction.
    Screenshot from 2021-08-10 16-55-46.png
    I want to remove the shortcut inside that red rectangle.

    eyllanescE 1 Reply Last reply
    0
    • PatitotectiveP Patitotective

      I'm trying to stop displaying the shortcut in the QAction.
      Screenshot from 2021-08-10 16-55-46.png
      I want to remove the shortcut inside that red rectangle.

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

      @Patitotective One possible trick is to use a QProxyStyle:

      import sys
      
      from PyQt5.QtGui import QKeySequence
      from PyQt5.QtWidgets import QApplication, QMainWindow, QProxyStyle, QStyle
      
      
      class CustomStyle(QProxyStyle):
          def drawControl(self, element, option, painter, widget=None):
              if element == QStyle.CE_MenuItem:
                  option.text = option.text.split("\t")[0]
              return super().drawControl(element, option, painter, widget)
      
      
      if __name__ == "__main__":
          app = QApplication(sys.argv)
      
          style = CustomStyle(app.style())
          style.setParent(app)
          app.setStyle(style)
      
          w = QMainWindow()
          menu = w.menuBar().addMenu("About")
          action = menu.addAction("About me")
          action.setShortcut(QKeySequence("Ctrl+M"))
          menu.addAction(action)
          w.show()
      
          sys.exit(app.exec_())
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      PatitotectiveP 1 Reply Last reply
      0
      • eyllanescE eyllanesc

        @Patitotective One possible trick is to use a QProxyStyle:

        import sys
        
        from PyQt5.QtGui import QKeySequence
        from PyQt5.QtWidgets import QApplication, QMainWindow, QProxyStyle, QStyle
        
        
        class CustomStyle(QProxyStyle):
            def drawControl(self, element, option, painter, widget=None):
                if element == QStyle.CE_MenuItem:
                    option.text = option.text.split("\t")[0]
                return super().drawControl(element, option, painter, widget)
        
        
        if __name__ == "__main__":
            app = QApplication(sys.argv)
        
            style = CustomStyle(app.style())
            style.setParent(app)
            app.setStyle(style)
        
            w = QMainWindow()
            menu = w.menuBar().addMenu("About")
            action = menu.addAction("About me")
            action.setShortcut(QKeySequence("Ctrl+M"))
            menu.addAction(action)
            w.show()
        
            sys.exit(app.exec_())
        
        PatitotectiveP Offline
        PatitotectiveP Offline
        Patitotective
        wrote on last edited by
        #3

        @eyllanesc It works but there is still a long whitespace.
        2bd4b869-1ab1-4229-a3c1-b330be7a4254-image.png
        There is a way to delete it?

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

          Hi,

          You can try with the QAction::shortcutVisibleInContextMenu property.

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

          PatitotectiveP 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            You can try with the QAction::shortcutVisibleInContextMenu property.

            PatitotectiveP Offline
            PatitotectiveP Offline
            Patitotective
            wrote on last edited by Patitotective
            #5

            @SGaist Take a look at this minimal example where I, using setShortcutVisibleInContextMenu, setted shortcutVisibleInContextMenu to False in close_action but the shortcut was still there:

            import sys
            from PyQt5.QtWidgets import (QApplication, 
               QAction, QMainWindow, qApp)
            
            class MainWindow(QMainWindow):
               def __init__(self, parent=None):
                  super().__init__(parent)
                  
                  self.create_menu_bar()
            
               def create_menu_bar(self):
                  # filling up a menu bar
                  bar = self.menuBar()
            
                  # File menu
                  file_menu = bar.addMenu('&File')
                  
                  close_action = QAction("Close", self)
                  close_action.setShortcut("Ctrl+Q")
                  close_action.setShortcutVisibleInContextMenu(False)
                  close_action.triggered.connect(self.close)
            
                  file_menu.addAction(close_action)
            
            if __name__ == '__main__':
               app = QApplication(sys.argv)
               # creating main window
               mw = MainWindow()
               mw.show()
               sys.exit(app.exec_())
            

            am i doing it the right way?

            JonBJ 1 Reply Last reply
            0
            • PatitotectiveP Patitotective

              @SGaist Take a look at this minimal example where I, using setShortcutVisibleInContextMenu, setted shortcutVisibleInContextMenu to False in close_action but the shortcut was still there:

              import sys
              from PyQt5.QtWidgets import (QApplication, 
                 QAction, QMainWindow, qApp)
              
              class MainWindow(QMainWindow):
                 def __init__(self, parent=None):
                    super().__init__(parent)
                    
                    self.create_menu_bar()
              
                 def create_menu_bar(self):
                    # filling up a menu bar
                    bar = self.menuBar()
              
                    # File menu
                    file_menu = bar.addMenu('&File')
                    
                    close_action = QAction("Close", self)
                    close_action.setShortcut("Ctrl+Q")
                    close_action.setShortcutVisibleInContextMenu(False)
                    close_action.triggered.connect(self.close)
              
                    file_menu.addAction(close_action)
              
              if __name__ == '__main__':
                 app = QApplication(sys.argv)
                 # creating main window
                 mw = MainWindow()
                 mw.show()
                 sys.exit(app.exec_())
              

              am i doing it the right way?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Patitotective
              Your code is "correct". However your QAction is on a menu/menubar. And setShortcutVisibleInContextMenu(), as its name states, only applies on a context menu (i.e. right-click popup).

              Unless @SGaist says otherwise, I don't think it will have any effect here. You might test this by trying your QAction in a context menu, instead of on a menubar.

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

                No I am not saying otherwise.

                Just checked the sources of QMenu to confirm and indeed, the property is checked together with the "contextuallity" of the menu.

                There's an additional check for \t in the action text and if so, the shortcut text should not appear.

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

                PatitotectiveP 1 Reply Last reply
                0
                • JonBJ JonB

                  @Patitotective
                  Your code is "correct". However your QAction is on a menu/menubar. And setShortcutVisibleInContextMenu(), as its name states, only applies on a context menu (i.e. right-click popup).

                  Unless @SGaist says otherwise, I don't think it will have any effect here. You might test this by trying your QAction in a context menu, instead of on a menubar.

                  PatitotectiveP Offline
                  PatitotectiveP Offline
                  Patitotective
                  wrote on last edited by Patitotective
                  #8

                  @JonB But what I'm trying to achieve is to hide the shortcut of a QAction in the QMenuBar not in a context menu. Also when is searched for context menu i found just a QMenu.
                  (QMenu nor QMenuBar has setShortcutVisibleInContextMenu method)

                  JonBJ 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    No I am not saying otherwise.

                    Just checked the sources of QMenu to confirm and indeed, the property is checked together with the "contextuallity" of the menu.

                    There's an additional check for \t in the action text and if so, the shortcut text should not appear.

                    PatitotectiveP Offline
                    PatitotectiveP Offline
                    Patitotective
                    wrote on last edited by
                    #9

                    @SGaist How should I remove the \t?

                    1 Reply Last reply
                    0
                    • PatitotectiveP Patitotective

                      @JonB But what I'm trying to achieve is to hide the shortcut of a QAction in the QMenuBar not in a context menu. Also when is searched for context menu i found just a QMenu.
                      (QMenu nor QMenuBar has setShortcutVisibleInContextMenu method)

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @Patitotective said in PyQt5 - How to stop displaying shortcut in QAction?:

                      @JonB But what I'm trying to achieve is to hide the shortcut of a QAction in the QMenuBar not in a context menu.

                      Which is exactly what I said: I don't see that setShortcutVisibleInContextMenu() is going to work in a QMenuBar, just look at the method name. Hence the (lack of) behaviour you reported when you tried it.

                      (QMenu nor QMenuBar has setShortcutVisibleInContextMenu method)

                      As @SGaist referenced for you, it's a method of QAction.

                      PatitotectiveP 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Patitotective said in PyQt5 - How to stop displaying shortcut in QAction?:

                        @JonB But what I'm trying to achieve is to hide the shortcut of a QAction in the QMenuBar not in a context menu.

                        Which is exactly what I said: I don't see that setShortcutVisibleInContextMenu() is going to work in a QMenuBar, just look at the method name. Hence the (lack of) behaviour you reported when you tried it.

                        (QMenu nor QMenuBar has setShortcutVisibleInContextMenu method)

                        As @SGaist referenced for you, it's a method of QAction.

                        PatitotectiveP Offline
                        PatitotectiveP Offline
                        Patitotective
                        wrote on last edited by Patitotective
                        #11

                        @JonB What i'm trying to do now is to create the shortcut independently of the QAction. But i also want to display the shortcut next to the QAction but it seems that QAction doesn't support html style (because it displays the html as text), i mean <span style="color: #ffffff; font-size: 14px;">Ctrl+Q</span>.
                        So my question now is, there is a way to make QAction supports html style or another style way?

                        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