Qt状态栏颜色
我将 Qt 与 Python 结合使用,并且有一个底部带有状态栏的主窗口。我可以使用 QLabel 在栏中显示一条消息,并使用类似 "In Progress"
的内容设置该消息的颜色QLabel 文本。
我还想在状态栏中放置一条临时消息,并为该消息指定颜色。然而,由于这次它不是 QLabel(我使用的是 QStatusBar::showMessage,它只需要一个 QString),我无法再更改颜色。无法识别上述标签,整个字符串 "In Progress"
显示为灰色。
有人有什么想法吗?
I'm using Qt with Python, and I've got a mainwindow with a status bar at the bottom. I can display a message in the bar using a QLabel, and set the color of that message using something like "<font color=\"green\">In progress</font>"
for the QLabel text.
I would like to also put a temporary message in the status bar, and assign a color to that message as well. However since it's not a QLabel this time (I'm using QStatusBar::showMessage which just takes a QString) I can't change the color anymore. The tags above are not recognized and the entire string "<font color=\"green\">In progress</font>"
is shown in gray.
Does anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要设置 QStatusBar 的背景或文本颜色,请在显示消息之前更改其样式表:
在 init 上,将 QStatusBar 的 messageChanged(QString) 连接到 statusChanged() 函数。
时间
To set the background or text color for a QStatusBar, change it's styleSheet before showing the message:
on init, connect the QStatusBar's messageChanged(QString) to a statusChanged() function.
T
不幸的是,
QStatusBar::showMessage()
不支持富文本格式。很久以前,这甚至被报告为功能请求 ,但似乎并没有引起足够的重视。我认为您最好的选择是坚持使用纯文本消息或直接操纵现有
QLabel
。这需要一些额外的工作来处理临时状态更改,因此您可以决定是否值得这样做。Unfortunately,
QStatusBar::showMessage()
doesn't support rich text formatting. This was even reported as a feature request long time ago, but it seems it didn't get enough attention.I think your best bet is to either stick with plain text messages or manipulate your existing
QLabel
directly. This would require some additional work to handle temporary status changes, so it's your call to decide if it's worth the trouble.如果您的 showMessages 文本全部采用相同的颜色,您可以通过 QtDesigner 在 QStatusBar 的调色板中定义它(窗口文本颜色)用于临时消息,然后使用 QLabel 颜色用于不同颜色的普通消息和永久消息。
If your showMessages text will be all of the same color, you can define it in the palette of QStatusBar via QtDesigner(window text color) for temporary messages, and then use the QLabel color for normal and permanent messages of different colors.
到目前为止我能找到的解决这个问题的最短解决方案是:
虽然它不是 100% 干净 - 如果在计时器运行时间的 2 秒内触发另一条此类消息,那么颜色可能会过早变回来。但实际上这几乎没有任何意义。
The shortest solution I could find for this problem so far:
It's not 100% clean though - if another message of this kind is triggered during the 2 seconds of the timer run time, then the color possibly changes back too early. But in practice this will hardly be of any relevance.
您还可以子类
QStatusBar
并实现“彩色”状态消息,例如(在 C++ 中):You can also subclass
QStatusBar
and implemented "colored" status messages, something like (in C++):在Python中使用
in Python use