Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPalette.HighlightedText not returning accurate value?



  • I am trying to get the color that will contrast well with QPalette.Highlight, which is supposed to be QPalette.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()
    

    Screen Shot 2019-10-18 at 9.09.23 AM.png


  • Lifetime Qt Champion

    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
    alt text

    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)


Log in to reply