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

Restore widget default palette color



  • I start life with widgets whose palette has not been altered, so they are at whatever the "default" is for the windowing system. I dynamically change (text color) like:

        QPalette palette = lineEdit->palette();
        palette.setColor(QPalette::Text, colour);
        lineEdit->setPalette(palette);
    

    At a later date I want to remove/restore it to the original default.

    What is the canonical way to do this? E.g.

    • I don't wish to have to save what the color was before I changed it.
    • I'm not seeing a "restore default color/palette", like say palette.setDefaultColor(QPalette::Text), that's what I thought there would be, is there something?
    • Do I have to do something like create a temporary QLineEdit to access its palette for the color to copy?

    EDIT Ah, I just saw the default QPalette::QPalette() says:

    Constructs a palette object that uses the application's default palette.

    That looks good! I thought it would be an "empty" one.

    Even better, QPalette QApplication::palette(const QWidget *widget), https://doc.qt.io/qt-5/qapplication.html#palette

    If a widget is passed, the default palette for the widget's class is returned. This may or may not be the application palette.

    So I think have answered my question, that's what I should use with my widget type to get back the original default color value to reset, right?



  • Even better, QPalette QApplication::palette(const QWidget *widget), https://doc.qt.io/qt-5/qapplication.html#palette

    If a widget is passed, the default palette for the widget's class is returned. This may or may not be the application palette.


  • Lifetime Qt Champion

    Hi,

    Looks good yes.



  • Even better, QPalette QApplication::palette(const QWidget *widget), https://doc.qt.io/qt-5/qapplication.html#palette

    If a widget is passed, the default palette for the widget's class is returned. This may or may not be the application palette.


Log in to reply