英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论