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. Change colors in menuBar()

Change colors in menuBar()

Scheduled Pinned Locked Moved Unsolved Qt for Python
10 Posts 3 Posters 2.0k Views
  • 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.
  • P Offline
    P Offline
    PythonQTMarlem
    wrote on last edited by
    #1

    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?

    jsulmJ 1 Reply Last reply
    0
    • P PythonQTMarlem

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @PythonQTMarlem Take a look at https://doc.qt.io/qt-6/stylesheet-examples.html
      "Customizing QMenuBar"

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

      1 Reply Last reply
      1
      • P Offline
        P Offline
        PythonQTMarlem
        wrote on last edited by
        #3

        Thank you,

        1 Reply Last reply
        0
        • P Offline
          P Offline
          PythonQTMarlem
          wrote on last edited by
          #4

          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?

          jsulmJ 1 Reply Last reply
          0
          • P PythonQTMarlem

            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?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

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

            1 Reply Last reply
            1
            • P Offline
              P Offline
              PythonQTMarlem
              wrote on last edited by
              #6

              okay! thank you very much!

              1 Reply Last reply
              0
              • P Offline
                P Offline
                PythonQTMarlem
                wrote on last edited by
                #7

                I'm confused.
                this works:

                style += "background-color: black; color: white;"
                

                this works not:

                style += "QMainWindow {background-color: black; color: white;}"
                

                what is wrong?

                jsulmJ JonBJ 2 Replies Last reply
                0
                • P PythonQTMarlem

                  I'm confused.
                  this works:

                  style += "background-color: black; color: white;"
                  

                  this works not:

                  style += "QMainWindow {background-color: black; color: white;}"
                  

                  what is wrong?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @PythonQTMarlem said in Change colors in menuBar():

                  QMainWindow

                  Are you applying this on a QMainWindow instance?

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

                  1 Reply Last reply
                  0
                  • P PythonQTMarlem

                    I'm confused.
                    this works:

                    style += "background-color: black; color: white;"
                    

                    this works not:

                    style += "QMainWindow {background-color: black; color: white;}"
                    

                    what is wrong?

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

                    @PythonQTMarlem
                    When you show you do self.setStyleSheet(style), what widget is self?

                    QMainWindow only applies to a QMainWindow. Are you expecting it to apply to a menubar?

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      PythonQTMarlem
                      wrote on last edited by PythonQTMarlem
                      #10

                      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 += )

                      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