PyQt5 - How to stop displaying shortcut in QAction?
-
I'm trying to stop displaying the shortcut in the QAction.
I want to remove the shortcut inside that red rectangle. -
@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_())
-
@eyllanesc It works but there is still a long whitespace.
There is a way to delete it? -
Hi,
You can try with the QAction::shortcutVisibleInContextMenu property.
-
@SGaist Take a look at this minimal example where I, using
setShortcutVisibleInContextMenu
, settedshortcutVisibleInContextMenu
toFalse
inclose_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?
-
@Patitotective
Your code is "correct". However yourQAction
is on a menu/menubar. AndsetShortcutVisibleInContextMenu()
, 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. -
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. -
@JonB But what I'm trying to achieve is to hide the shortcut of a
QAction
in theQMenuBar
not in a context menu. Also when is searched for context menu i found just aQMenu
.
(QMenu
norQMenuBar
hassetShortcutVisibleInContextMenu
method) -
@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 aQMenuBar
, 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
. -
@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 theQAction
but it seems thatQAction
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 makeQAction
supports html style or another style way?