我正在编写一个PyQt系统托盘脚本。它只是一个系统服务的交换机。我通过以下代码将QActions添加到QMenu中,我的目的是显示正在运行的服务为绿色,停止的服务为红色:
....
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))
.... 问题是,QAction没有setFontColor方法:)。它有一个setFont方法,但我在QFont文档中看不到与颜色相关的方法。而且它不支持富文本格式。
我找到了一个可能的解决方案here,但这个简单的操作似乎有太多的工作要做。
有没有人能给我一个更简单的建议?
发布于 2011-08-16 13:16:44
我能看到的唯一更简单的方法是让你的QAction可检查(例如,定义“服务是活动的”应该检查该项),然后使用Qt样式表来获得你想要的效果。
可以在以下位置找到样式菜单项的示例:Qt Style Sheets - Customizing QMenu
发布于 2011-08-16 22:26:17
这并不是你想要的,但是你可以非常简单地将与QAction相关的图标更改为一个红色或绿色的圆点:这样菜单文本不会改变颜色,但是圆点会改变颜色。
....
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))
.... https://stackoverflow.com/questions/7071796
复制相似问题