utdemir
utdemir

Reputation: 27216

QApplication Font Color

I'm writing a PyQt systemtray script. It simply a switch for system services. I'm adding QActions to QMenu via this code, my purpose is showing running services green and stopped services red:

....
for service, started in s.services.items():
    action = self.menu.addAction(service)

    if started: #It is my purpose, but obviously it doesn't work
        action.setFontColor((0, 255, 0))
    else:
        action.setFontColor((255, 0, 0))

    action.triggered.connect(functools.partial(self.service_clicked, service))
....    

The problem is, QAction's don't have a setFontColor method :). It has a setFont method but I couldn't see a color related method in QFont documentation. And it doesn't support rich-text formatting.

I found a possible solution here, but it seems so much work for this simple operation.

Can anybody suggest me a simplier way?

Upvotes: 0

Views: 394

Answers (2)

docsteer
docsteer

Reputation: 2516

Not exactly what you want, but you could change the icon associated with the QAction to e.g. a red or green dot very simply : so the menu text wouldn't change colour but the dot would.

....
for service, started in s.services.items():
    action = self.menu.addAction(service)

    if started: #It is my purpose, but obviously it doesn't work
        action.setIcon(QIcon(":/greendot.png"))
    else:
        action.setIcon(QIcon(":/reddot.png"))

    action.triggered.connect(functools.partial(self.service_clicked, service))
....    

Upvotes: 1

Mat
Mat

Reputation: 206679

The only simpler way I can see is to make your QActions checkable (and define for instance that "service is active" should check the item), and then play with Qt style sheets to get your desired effect.

Examples of styling menu items can be found here: Qt Style Sheets - Customizing QMenu

Upvotes: 1

Related Questions