How to set brush in QML code
-
Hey guys
I tried to find an example, but I didn't find anything.
In ScatterSeries there is a property called brush.
I want to define a color and fill pattern directly in QML code, such as ("red", Qt.SolidPattern).
If property exists, I believe there is a way to do this.
Does anyone have any example of how to set this property? -
@VieiraFilho said in How to set brush in QML code:
If property exists, I believe there is a way to do this
not necessarily. AFAIK QBrush is not contained in the QML engine as a type so you have to use C++.
You can either pass the ScatterSeries object to an invokable C++ method (accepting a QObject* or QScatterSeries* parameter) and fill it there. Or you create an invokable C++ method which returns a QBrush from your call parameters packed in a QVariant (You must register the QBrush type with the meta type system).
I am not quite sure if the second approach really works though, but try it. -
-
@eyllanesc said in How to set brush in QML code:
@raven-worx like this https://stackoverflow.com/a/59009525/6622587
:/
Definitely try the second method (a helper method returning a QBrush) before modifying your UI objects from your business code.
-
@GrecKo said in How to set brush in QML code:
before modifying your UI objects from your business code
i was talking about modifying the ScatterSeries object, which actually acts as a model for charts if you want so.
-
It's not only a model, it's also a representation of the data. I maintain that you should not modify that in C++.
The solution to OP's problem is the following:
Create a class like that in C++
class BrushFactory : public QObject { Q_OBJECT public: Q_INVOKABLE QBrush brushFromColor(const QColor& color) // method returning a "fancy" brush { return QBrush(color, Qt::Dense4Pattern); } };
Register it as a singleton type to the QML engine, and us it in QML like so:
ScatterSeries { // ... brush: BrushFactory.brushFromColor("pink") // ... }
Note that if you just want to set a simple color on the series, it already has a
color
property. -