Change colors in menuBar()
-
Hello,
pyqt6. I simulate the high contrast:def set_style(self, font_size=None, high_contrast=False): """ This method allows customizing the application style (e.g. font size, high contrast). :param font_size: The font size. :param high_contrast: A flag to enable high contrast mode. """ style = '' if font_size: style += f'font-size: {font_size}px;' if high_contrast: style += 'background-color: black; color: white;' # Add a white border to the QPushButton elements style += 'border: 2px solid white;' self.current_stylesheet = style self.setStyleSheet(style) I would like to change the colors of a selected menu entry in high contrast: self.menu_bar = self.menuBar() # Main menu item "Program" with submenus self.program_menu = self.menu_bar.addMenu("Program") # “Execute command” submenu self.run_action = self.program_menu.addAction("Execute command") self.run_action.triggered.connect(self.execute_command) self.run_action.setShortcut("F2") self.run_action.setStatusTip("Execute command")
Ask:
Is that possible? -
@PythonQTMarlem Take a look at https://doc.qt.io/qt-6/stylesheet-examples.html
"Customizing QMenuBar" -
Thank you,
-
I need help.
I tried:def set_style(self, font_size=None, high_contrast=False): style = '' if font_size: style += f'font-size: {font_size}px;' if high_contrast: # Füge das allgemeine Styling für den Hochkontrastmodus hinzu style += """ QMenu { background-color: white; margin: 2px; /* some spacing around the menu */ } QMenu::item { padding: 2px 25px 2px 20px; border: 1px solid transparent; /* reserve space for selection border */ } QMenu::item:selected { border-color: darkblue; background: rgba(100, 100, 100, 150); } """ # Anwendungs-Styling style += """ QMainWindow { background-color: black; /* Hintergrundfarbe der Anwendung */ } QMainWindow QLabel { color: white; /* Schriftfarbe der Anwendung */ } QPushButton { background-color: yellow; color: black; border: 2px solid white; } """ print(style) self.current_stylesheet = style self.setStyleSheet(style)
The entire CSS code is not implemented. I don't see any changes in my pyqt6 application. Question: what am I doing wrong?
-
@PythonQTMarlem Start with a minimum style sheet and add one piece at a time until it stops working, then you know which part is wrong.
-
okay! thank you very much!
-
I'm confused.
this works:style += "background-color: black; color: white;"
this works not:
style += "QMainWindow {background-color: black; color: white;}"
what is wrong?
-
@PythonQTMarlem said in Change colors in menuBar():
QMainWindow
Are you applying this on a QMainWindow instance?
-
@PythonQTMarlem
When you show you doself.setStyleSheet(style)
, what widget isself
?QMainWindow
only applies to aQMainWindow
. Are you expecting it to apply to a menubar? -
this code:
if high_contrast: style += " QMainWindow {background-color: black; color: white;}" print(style) self.main_window.setStyleSheet(style)
output print: font-size: 30px; QMainWindow {background-color: black; color: white;}
but this works:
if high_contrast: style = " QMainWindow {background-color: black; color: white;}" print(style) self.main_window.setStyleSheet(style)
output print:
QMainWindow {background-color: black; color: white;}the problem just appears with += . (style += )