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

Palette colors of QLineEdit widget disappear on inactive window



  • Hello,

    I am successfully setting the background of a QLineEdit widget for a status indication using:

    ... create brushes for the roles QPalette.WindowText, QPalette.Window and then...
    widget.setPalette(palette)

    My problem is that when the user clicks outside of the main app window (makes the app the inactive window), the colors for the QLineEdit widget reverts back to its original color palette. When the window is click on (made the active window), the updated color palette reappears on the QLineEdit widget.

    Is this behavior because widget.setAutoFillBackground(True) ?

    What am I missing here?

    Thank you for any insight and advice.


  • Lifetime Qt Champion

    Hi
    I think it goes back to normal looking when it looses focus as u only set
    QPalette.Active
    and docs says
    The color groups:
    The Active group is used for the window that has keyboard focus.
    The Inactive group is used for other windows.

    so i think u need to set for inactive too to keep look when user click outside.
    (and keyboard focus goes else where)


  • Moderators

    @David-Jacobson
    how do you exactly set the brushes to the palette?
    Note the QPalette::ColorGroup parameter in the setBrush() method!



  • @raven-worx

    Thanks for the tip! Below is my method for setting the brush. I thought it was the basic way of doing it, but, I will look into using the QPalette::ColorGroup.

    palette = QPalette()
    
    brush = QBrush(QColor(pR, pG, pB))
    brush.setStyle(Qt.SolidPattern)
    palette.setBrush(QPalette.Active, QPalette.Window, brush)
    
    pWidget.setPalette(palette)

  • Lifetime Qt Champion

    Hi
    I think it goes back to normal looking when it looses focus as u only set
    QPalette.Active
    and docs says
    The color groups:
    The Active group is used for the window that has keyboard focus.
    The Inactive group is used for other windows.

    so i think u need to set for inactive too to keep look when user click outside.
    (and keyboard focus goes else where)



  • @raven-worx @mrjj

    And your tips provided my solution. Thank you. I simply added the following setbrush...

    palette.setBrush(QPalette.Inactive, QPalette.Window, brush)


Log in to reply