Search code examples
python-3.xpyqt4qtstylesheets

Styling The Menu Bar in PyQT4


I am finishing up my application and I can't figure up how to change the MenuBar in pyqt4. I am using a dark and gray theme, and on windows the menu bar is white, and I would like it to be dark like the rest of the app. How do I change the background color of QMenu or QMenuBar colors In PyQt4. I have been able to change the drop downs, but the top bar with File | Tools | Help stays white. Properties I tried changing:

background-color: # Doesn't seem to do anything
color: # Only changes the text color not the background
alternate-background-color: # Doesn't seem to do anything

Maybe I just haven't found the right property to assign the background color to match the rest of the app, a little help? Thanks!


Solution

  • It looks fine on my PC.

    class SubMenu(QMenuBar):
        def __init__(self, parent=None):
            super(SubMenu, self).__init__(parent)
            self.addAction("File")
            self.addAction("View")
            self.setStyleSheet("""QMenuBar {
                 background-color: blue;
            }""")
            self.resize(320, 240)
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        m = SubMenu()
        m.show()
        app.exec_()
    

    Style sheet with Items

    class SubMenu(QMenuBar):
        def __init__(self, parent=None):
            super(SubMenu, self).__init__(parent)
            self.addAction("File")
            self.addAction("View")
            self.setStyleSheet("""QMenuBar {
             background-color: blue;
            }
    
         QMenuBar::item {
             background: blue;
         }""")
            self.resize(320, 240)
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        m = SubMenu()
        m.show()
        app.exec_()
    

    This is stylesheet without menu item This is stylesheet with Item