Search code examples
windowsqtcolorshighlightqlistwidget

How does QListWidget set black or white selected text?


Standard QListWidget draws white text of selected row on Windows XP and black on Windows 7/8. Where does it get black or white color?

I've made a custom delegate for ListView and try to draw text in a selected row, but application palette returns same colors (HighlightedText, BrightText, WindowText, Text) on WinXP and 8, while default delegate somehow draws it black or white. I've looked through Qt source and couldn't find where does it get the contrast color. QListWidget style itself returns the same color of ForegroundRole on all platforms, too.

Am I missing some way of getting correct os palette in Qt app?


Solution

  • Unfortunately not all colors are readable via QPalette. Especially for the QAbstractItemView subclasses (list views, tree views…), the Windows style retrieves the colors via WinAPI and uses them directly for painting.

    All relevant code should be in qtbase/src/widgets/styles/qwindowsxpstyle.cpp and qtbase/src/widgets/styles/qwindowsvistastyle.cpp, search for "pGetThemeColor”.

    As Marek says, for some of these colors it’s possible to override those defaults, but it’s not possible to read them via Qt API.