如何获取Qt6中小部件的主题颜色?

huangapple go评论85阅读模式
英文:

How Do I get theme colors of widgets in Qt6

问题

我正在尝试检索Qt6中各种小部件的主题颜色。我已经使用了QApplication.style(),但我不确定如何从返回的QCommonStyle对象中获取所需的颜色。具体来说,我需要获取QLabel的文本颜色,QListView的背景颜色等等。目前,我的方法涉及创建临时对象以检索所需的颜色,但这是一个不优雅的解决方法。

更新:

我找到了解决方案:

可以通过QPalette访问颜色。

if option.state & QStyle.State_Active:
	if option.state & QStyle.State_Selected:
		textColor = QPalette.HighlightedText
		brightTextColor = QPalette.HighlightedText
	else:
		textColor = QPalette.WindowText
		brightTextColor = QPalette.BrightText
else:
	textColor = QPalette.WindowText
	brightTextColor = QPalette.BrightText
英文:

I'm attempting to retrieve the theme colors of various widgets in Qt6. I've utilized QApplication.style(), but I'm uncertain as to how to obtain the colors I need from the QCommonStyle object that is returned. Specifically, I require the text color of a QLabel, the background color of a QListView, and so on. Currently, my approach involves creating temporary objects in order to retrieve the desired colors, but this is an inelegant workaround.

Update:

I've found the solution:

The colors are accessible through QPalette.

if option.state & QStyle.State_Active:
	if option.state & QStyle.State_Selected:
		textColor = QPalette.HighlightedText
		brightTextColor = QPalette.HighlightedText
	else:
		textColor = QPalette.WindowText
		brightTextColor = QPalette.BrightText
else:
	textColor = QPalette.WindowText
	brightTextColor = QPalette.BrightText

答案1

得分: 0

我自己找到了解决方案。您可以在更新的内容中找到它。

英文:

I've found a solution myself. You can find it above in the Update.

huangapple
  • 本文由 发表于 2023年6月16日 13:17:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定