QPalette.HighlightedText not returning accurate value?
-
I am trying to get the color that will contrast well with
QPalette.Highlight, which is supposed to beQPalette.HighlightedText. However, the value returned in both the app or widget palette is not the value used in painting (Qt-5.13.1).Surely there is something I am doing wrong?
app = QApplication(sys.argv) w = QListWidget() w.addItem('Here') w.addItem('You') w.addItem('Are') w.show() palette = w.palette() SELECTED_TEXT_COLOR = palette.color(QPalette.HighlightedText) print('QPalette.HighlightedText:', SELECTED_TEXT_COLOR.name()) app.exec()
-
Hi
Nope, you are not doing anything wrong. :)
It seems to depend a bit on the Widget in Question, the QStyle used and platform if the palette is used for the actual drawing.So more often stylesheets work better.
When i want to use palette, i normally test it first
QPalette p = ui->listWidget->palette(); for (int cr = QPalette::ColorRole::WindowText; cr < QPalette::ColorRole::PlaceholderText; ++cr) { p.setColor( QPalette::ColorRole(cr), Qt::red); } ui->listWidget->setPalette(p);This changes all colors so its easy to see if it will work. ( on that platform)
so here on ListWidget ( windows platform) does seems to use it

However, i can also see some effect is added to the selected drawing. as else it would not be visisble.
Also as a note. you are taking the system palette.
Also test the Widgets one (just to be sure)
SELECTED_TEXT_COLOR = w.palette.color(QPalette.HighlightedText)