How do you get the window and text color from a qss stylesheet
-
The program I am contributing to, determines the icon color depending on this.
-
Hi,
It's usually the other way around, you grab that information, insert it in your QSS and then apply it.
-
Yeah, but I'm asking for the reverse
-
So you want to parse the style sheet to find the information ?
-
@SGaist
Yes, I do -
On first thought, I don't think you need to do anything with the style sheet directly. I suspect that you want to show your icon on a button or label. So, first create that button or label and try to figure out what the respective colors are for this widget.
https://doc.qt.io/qt-6/stylesheet.html says the following at the end:
When a style sheet is active, the QStyle returned by QWidget::style() is a wrapper "style sheet" style, not the platform-specific style. The wrapper style ensures that any active style sheet is respected and otherwise forwards the drawing operations to the underlying, platform-specific style (e.g., QWindowsVistaStyle on Windows).
Maybe the QStyle of your label/button will help here. But, on first look, I didn't find any information how to extract the colors from the QStyle.
-
On first thought, I don't think you need to do anything with the style sheet directly. I suspect that you want to show your icon on a button or label. So, first create that button or label and try to figure out what the respective colors are for this widget.
https://doc.qt.io/qt-6/stylesheet.html says the following at the end:
When a style sheet is active, the QStyle returned by QWidget::style() is a wrapper "style sheet" style, not the platform-specific style. The wrapper style ensures that any active style sheet is respected and otherwise forwards the drawing operations to the underlying, platform-specific style (e.g., QWindowsVistaStyle on Windows).
Maybe the QStyle of your label/button will help here. But, on first look, I didn't find any information how to extract the colors from the QStyle.
I already can display the icon, I just need to get the color off the .qss for the widget thats it.
-
@TeamGregor said in How do you get the window and text color from a qss stylesheet:
@SimonSchroeder
I already can display the icon, I just need to get the color off the .qss for the widget thats it.The QStyle of the button or label will have parsed the style sheet already and will give you the proper colors (if you can get the colors from the QStyle, which I didn't find quickly). The problem is that you can have multiple style sheets on multiple levels and Qt will combine them in the proper way. There is no way to query the combined style sheet at the current level.
If you want to instead use the colors you have in the main style sheet applied to the QApplication (and ignore any other style sheets), then I would suggest to just use those colors and not parse them back from the style sheet. The only reason to query them from the style sheet at runtime is if the user can somehow manipulate the colors. But then again, store the colors separately when the user changes them (if he does it through a UI).