开发者

QApplication Font Color

开发者 https://www.devze.com 2023-03-28 12:40 出处:网络
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:

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?


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


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))
....    
0

精彩评论

暂无评论...
验证码 换一张
取 消