PyQt5 - QWidgetAction doesn't working when adding html style
-
I'm using
QWidgetAction
because usingQAction
i can't add stylesheet. But for some reason usingQWidgetAction
the shortcut isn't displayed next to it, so decided to add my own.
I created this function to createQWidgetAction
widgets:def create_action_widget(self, text: str, menu, stylesheet: str="", callback: callable=lambda: print("No callback"), shortcut: str=""): """ Args: text (str): The text to be displayed in the QWidgetAction. menu (menuBar.addMenu): The menu to append the QWidgetAction. stylesheet (str, optional=""): The stylesheet of the QWidgetAction (really the stylesheet of the Label). callback (callable, optional=lambda: print("No callback")): The callback function to the QWidgetAction. shortcut (str, optional=""): The shortcut to acces to the QWidgetAction """ action = QWidgetAction(self) # Create a QWidget action in QMainWindow if shortcut != "": # If the shortcut isn't empty key = shortcut.split("+")[0] # Split by + and get the modifier key (ctrl, shift, alt) mnemo = shortcut.split("+")[1] # Split by + and get the key (a, b, c) ### IF I COMMENT THESE THREE LINES THE QWIDGETACTIONS WORKS ### text = text.replace(mnemo, f"<u>{mnemo}</u>", 1) # Underline the shortut letter text += f"{' '*3}" # Spacing between given text and shortcut text += f"<span style='color: #ABABAB; font-size: 14px;'>{key}+{mnemo.capitalize()}</span>" # Add different color and font-size to the shortcut label = QLabel(text) # Create a QLabel with the text label.setStyleSheet(stylesheet) # Set the given stylesheet to the QLabel action.setDefaultWidget(label); # Set QLabel as default widget to QWidget action.setShortcut(shortcut) # Set shortcut to QWidgetAction menu.addAction(action) # Add action to given menu (menuBar.addMenu) action.triggered.connect(callback) # Connect the QWidgetAction with the given callback return action # Return the QWidgetAction
But when i added the underlined, the spacing and the different color to the shortcut the callback function when clicking stop working, if i try the shortcut it works but clicking directly into the QWidgetAction doesn't work.
The whole file is public in my GitHub repo.
If you want to test the app, make sure you have the requirements. -
@Patitotective said in PyQt5 - QWidgetAction doesn't working when adding html style:
@jeremy_k I'm sorry, i don't understand what you're saying. You mean that i have to apply the stylesheet to which widget?
Apply the style sheet to the widget displaying the QAction. If you put the QAction in a QMenu, style that QMenu (or a broader selector that will apply to the menu).
-
QAction doesn't accept a style sheet because it might be displayed in multiple widgets (QMenu, QMenuBar, ...), or not at all. To style the display of the action, apply the style sheet to the widget.
-
@Patitotective said in PyQt5 - QWidgetAction doesn't working when adding html style:
@jeremy_k I'm sorry, i don't understand what you're saying. You mean that i have to apply the stylesheet to which widget?
Apply the style sheet to the widget displaying the QAction. If you put the QAction in a QMenu, style that QMenu (or a broader selector that will apply to the menu).
-
@jeremy_k Applying this stylesheet to the menu, hover is not working.
*{ background: #383838; color: #ABABAB; /*padding: 4px 4px;*/ font-size: 15px; } *:hover{ background: #4C4C4C; color: #ffffff; } *:pressed { background: #595959; }
-
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() bar.setStyleSheet( """ QMenuBar { background-color: #383838; } QMenuBar::item { padding: 1px 4px; background: transparent; border-radius: 4px; color: #ffffff } QMenuBar::item:selected { /* when selected using mouse or keyboard */ background: #4C4C4C; } QMenuBar::item:pressed { background: #595959; } """) menu_stylesheet = """ *{ background: #383838; color: #ABABAB; /*padding: 4px 4px;*/ font-size: 15px; } *:hover{ background: #4C4C4C; color: #ffffff; } *:pressed { background: #595959; } """ # File menu file_menu = bar.addMenu('&File') file_menu.setStyleSheet(menu_stylesheet) close_action = QAction("Close", self) close_action.setShortcut("Ctrl+Q") 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_())
-
@Patitotective Fixed using:
menu_stylesheet = ( "QMenu {" " background: #383838;" " color: #ABABAB;" " font-size: 15px;" "}" "QMenu::item:selected {" " background: #4C4C4C;" " color: #ffffff;" "}" "QMenu::item:pressed {" " background: #595959;" "}" )