Adam Sirrelle
Adam Sirrelle

Reputation: 397

PyQT QAction/QMenu color

I'm trying to find a way to set the items of a menu different colours.

Menu Items

So, for example, Web Guide is blue, Email Support is red, Version 1.0 is transparent.

I had a look for a while on how to do this, and tried out a Qlabel and QWidgetAction, but that seemed to just act as text, and not in the same way as an action.

I have a feeling that this is more pain then it's worth, but thought i'd throw it out there all the same.

This is how I have the style sheet and actions set up:

    self.setStyleSheet("""
        QMenuBar {
            background-color: rgb(49,49,49);
            color: rgb(255,255,255);
            border: 1px solid ;
        }

        QMenuBar::item {
            background-color: rgb(49,49,49);
            color: rgb(255,255,255);
        }

        QMenuBar::item::selected {
            background-color: rgb(30,30,30);
        }

        QMenu {
            background-color: rgb(49,49,49);
            color: rgb(255,255,255);
            border: 1px solid ;
        }

        QMenu::item::selected {
            background-color: rgb(30,30,30);

        }
    """)

    self.XactionHelpFAQ = QtGui.QAction('Web Guide', self)
    self.XactionHelpEmail = QtGui.QAction('Email Support', self)
    self.XactionHelpVersion = QtGui.QAction('Version 1.0', self)

    self.Xmenubar = QtGui.QMenuBar(self)
    self.Xmenubar.setObjectName('menubar')
    self.Xmenubar.setGeometry(QtCore.QRect(0, 0, 684, 21))
    self.Xmenubar.setSizeIncrement(QtCore.QSize(0, 0))
    self.Xmenubar.setDefaultUp(False)

    self.XmenuHelp = QtGui.QMenu('Help', self.Xmenubar)
    self.XmenuHelp.addAction(self.XactionHelpFAQ)
    self.XmenuHelp.addAction(self.XactionHelpEmail)
    self.XmenuHelp.addAction(self.XactionHelpVersion)

Upvotes: 1

Views: 3440

Answers (1)

syedelec
syedelec

Reputation: 1300

If you want to apply a stylesheet, you need to apply it on the widget you are 'styling'.

self.Xmenubar = QtGui.QMenuBar(self)
[...]
self.Xmenubar.setStyleSheet("""
    QMenuBar {
        background-color: rgb(49,49,49);
        color: rgb(255,255,255);
        border: 1px solid ;
    }

    QMenuBar::item {
        background-color: rgb(49,49,49);
        color: rgb(255,255,255);
    }

    QMenuBar::item::selected {
        background-color: rgb(30,30,30);
    }
""")

self.XmenuHelp = QtGui.QMenu('Help', self.Xmenubar)
[...]
self.XmenuHelp.setStyleSheet("""
    QMenu {
        background-color: rgb(49,49,49);
        color: rgb(255,255,255);
        border: 1px solid ;
    }

    QMenu::item::selected {
        background-color: rgb(30,30,30);

    }
""")

In most cases, you simply need to apply the general stylesheet to the QApplication object and apply some specific stylesheet to Qt object that will override the stylesheet applied to QApplication

Upvotes: 1

Related Questions