Qt's StyleSheet system is not valid QPalette; how to make them work together?

In one of my projects, I have a QwtPlot where I want to harmoniously adjust its colors, that is, for example, if the background color is white, the axis should be black, etc. Now I could do it "hardcoded": when I change the background (link) color, I go through an array of previously defined colors for an axis and set the given color in the array for that axis. But this is pretty out of fashion and I would like a more automated way.

Looking for help here , Uwe told me to use QwtPlot::setPalette(color)

and it works great on its own. The problem is that QwtPlot is a child of a series of QWidget-based widgets whose colors need to be adjusted in the global stylesheet file, and I noticed that when the stylesheet for one of those widgets is configured, it invalidates the QwtPlot call for setPalette

. It's as if I had to choose between them: if I use at least one call setPalette

in a given widget, then none of its parents before the main widget (QMainWindow in my case) should be configured with a stylesheet system. Seems to be what this piece of documentation says about setPalette

:

Warning. Do not use this feature in conjunction with Qt stylesheets. ( source )

but it looks like this should only happen when using a global call setPalette

.

So my questions are: is there a way to solve this problem in a harmonious way? Or do I have to ditch the style sheet system for this piece of software and use only palettes instead? I tried to tweak the stylesheet configuration in a more "localized" way that tries to tell the system "this stylesheet configuration is only valid for this widget, does not perpetuate its child widgets", with no success. If this is indeed possible (and therefore I was probably using the wrong syntax), I would like to know.

+3


source to share


1 answer


For stylesheet use .QWidget {...}

, not QWidget {...}

for styles that you don't want subclasses to inherit. If applicable to all parents QwtPlot

, it will allow you to use again setPalette

.

Nothing will save you, however, if you try to mix stylesheets and palettes for the same widget, the stylesheet will always replace it, so be careful!

EDIT:

the other is subclassing QwtPlot

and uses assignable properties for the canvas, allowing style sheets to be used to set the property, but also have programmatic access to the value.



#include <QWidget>
#include <QBrush>    
#include <qwt_plot.h>

class QPlot : public QwtPlot
{
    Q_OBJECT

    Q_PROPERTY(QBrush canvasBackground READ canvasBackground 
        WRITE setCanvasBackground DESIGNABLE true)

public:

    QPlot(const QwtText &title, QWidget* parent = nullptr) : QwtPlot(title, parent);

    void setCanvasBackground(const QBrush &brush) 
    { 
        QwtPlot::setCanvasBackground(brush); 
        // set the axes color as well, maybe YIQ method?
    };
    QBrush canvasBackground() const
    {
         return QwtPlot::canvasBackground;
    }
};

      

In your stylesheet, you can set the background of the canvas with

qproperty-canvasBackground: #000000;

      

The contrast color algorithm, in my opinion, is a little off the mark for this answer, but these questions might help: Select high contrast colors programmatically , How to invert color / color?

+1


source







All Articles