keirebu
keirebu

Reputation: 337

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!

Upvotes: 1

Views: 5086

Answers (2)

OnceACurious
OnceACurious

Reputation: 397

Hope this comment may help others someday. PySide2, Python 3+

self.saveFile_action = QAction("&Save", self)
self.saveFile_action.setShortcut("Ctrl+S")
self.saveAllFile_action = QAction("Save &All", self)
self.saveAllFile_action.setShortcut("Ctrl+Shift+S")

self.menuBar = QMenuBar(self)
self.menuBar.setStyleSheet(
"""
QMenuBar
{
    background-color: #333399;
    color: #999;
}
QMenuBar::item
{
    background-color: #333399;
    color: #999;
}
QMenuBar::item::selected
{
    background-color: #3399cc;
    color: #fff;
}
QMenu
{
    background-color: #3399cc;
    color: #fff;
}
QMenu::item::selected
{
    background-color: #333399;
    color: #999;
}
 """
)

self.fileMenu = QMenu("&File", self.menuBar)

self.exportSubmenu = QMenu("&Export", self.fileMenu)

self.fileMenu.addSeparator()
self.fileMenu.addAction(self.saveFile_action)
self.fileMenu.addAction(self.saveAllFile_action)
self.fileMenu.addSeparator()
self.fileMenu.addAction(self.exportSubMenu.menuAction())

self.settinsMenu = QMenu("&Settings", self.menuBar)
self.helpMenu = QMenu("&Help", self.menuBar)

self.menuBar.addAction(self.fileMenu.menuAction())
self.menuBar.addAction(self.settingsMenu.menuAction())
self.menuBar.addAction(self.helpMenu.menuAction())

enter image description here

Upvotes: 2

Cui Heng
Cui Heng

Reputation: 1285

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

Upvotes: 3

Related Questions