QPalette not inherited properly?



  • I'm trying to build a dark version of my application.
    I chose to work only with the QPalette but I'm facing a problem :
    Usin the quick color seletion, it changes from this
    !http://imgur.com/vgWKfOg.png!
    to this
    !http://imgur.com/pxloUuo.png!

    Note the preview is absolutely broken
    !http://i.imgur.com/ihgODSf.png!

    So for instance the QSpinBox are still using the original palette when they sould inherit the one from the MainWindow.
    Now if I change the palette of each element (and set the same one as the MainWindow) I get this :
    !http://i.imgur.com/5UsYe0m.png!

    It looks better but I don't understand why, since it should look the same than when the widget inherit their palette from the MainWindow.



  • AFAIK, the default application style doesn't only use the palette to retrieve the application colors.

    You may switch the style to Fusion. That style uses only the palette, so it's fully configurable. I've even recopilated a dark version: "here is a gist.":https://gist.github.com/Skyrpex/5547015

    Feel free to contribute (it's not finished :P).

    @qApp->setStyle(QStyleFactory::create("fusion"));

    QPalette palette;
    palette.setColor(QPalette::Window, QColor(53,53,53));
    palette.setColor(QPalette::WindowText, Qt::white);
    palette.setColor(QPalette::Base, QColor(15,15,15));
    palette.setColor(QPalette::AlternateBase, QColor(53,53,53));
    palette.setColor(QPalette::ToolTipBase, Qt::white);
    palette.setColor(QPalette::ToolTipText, Qt::white);
    palette.setColor(QPalette::Text, Qt::white);
    palette.setColor(QPalette::Button, QColor(53,53,53));
    palette.setColor(QPalette::ButtonText, Qt::white);
    palette.setColor(QPalette::BrightText, Qt::red);

    palette.setColor(QPalette::Highlight, QColor(142,45,197).lighter());
    palette.setColor(QPalette::HighlightedText, Qt::black);
    qApp->setPalette(palette);@



  • Thanks for your answer.

    1. I know that some things like QPushButton cannont be style with QPalette. However QSpinBox correctly use the palette but fails to inherit it, which is my point here.

    2. Fusion. Does it work with Qt4 or is it Qt5 only?



  • I guess it's only Qt5. Not sure at all...

    Anyway, it's true that the subwidgets should inherit the palette. I've never edited the palette in Qt4 so I don't have a clue if that's a bug in your code or Qt itself.

    Regards.



  • I found this:
    http://code.google.com/p/fusion-qt4/

    Will try your palette!

    Found this palette similar to yours:
    https://gist.github.com/QuantumCD/6245215



  • Was there ever a solution for this?

    In Qt5/Linux I'm experiencing the same behavior when dynamically switching between different palette colors, when the app is open.

    I was hoping to allow the user to switch color sets or customize colors through an in-app menu, but I see the same weirdness with certain elements not correctly re-coloring.

    If I start the app from scratch, with the same color sets, it all works fine.

    Is there a way to apply the palette dynamically?



  • IMO, you cannot change palette colors just like that under ms windows, for non windows (redmond) theme, since QStyle uses native mswindows theme to paint controls (eg. under windows xp theme, you cannot change color of QPushButton, but under redmond theme you can - of course I'm talking about Qt4 here, but probably same applies to Qt5). Under X11 you can do such things and palette is applied as you want.


  • Moderators

    Hi Julien,

    bq. I chose to work only with the QPalette

    I was wondering why you want to use qpalette only? Why are stylesheets not an option?



  • Because with a QPalette you can easily set a few colors and theme the whole application (at least this is how it used to work). Stylesheet requires much more work (look at the QDarkStyle stylesheet and you'll see they had to rewrite the style of almost all widgets)


  • Moderators

    thanks,

    I see your point.

    good luck with your investigations!



  • [quote author="Julien M" date="1396172572"]Because with a QPalette you can easily set a few colors and theme the whole application (at least this is how it used to work). Stylesheet requires much more work (look at the QDarkStyle stylesheet and you'll see they had to rewrite the style of almost all widgets)[/quote]

    Indeed, but you cannot do that on all themes as I already mentioned, so stylesheet could be only option in some cases (to be clear: I prefer QPalette over stylesheets in such cases, but it does not fit in all scenarios).



  • Found a solution to the issue of all child widgets not coloring properly:

    If you're using the Fusion them on Qt 5.x, which supports QPalette, the solution is to cycle through all the child widgets each time you switch the color theme.

    @ this->setPalette(palette);

    QList<QWidget*> widgets = this->findChildren<QWidget*>();
    
    foreach (QWidget* w, widgets)
    {
        w->setPalette(palette);
    }
    

    @

    QuantumCD posted the solution here: https://gist.github.com/QuantumCD/9863860


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.