QGradient Class
The QGradient class is the base of the gradients that brushes fill with. More…
| Header | #include <QGradient> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Gui) |
| Module | Nt GUI C++ Classes |
| Inherited by | QConicalGradient, QLinearGradient, QRadialGradient |
List of all members, including inherited members
Public Types
| enum | Type |
| enum | Spread |
| enum | CoordinateMode |
Public Functions
| QGradient() | |
| Type | type() const |
| void | setSpread(Spread spread) |
| Spread | spread() const |
| void | setColorAt(qreal position, const QColor &color) |
| void | setStops(const QGradientStops &stops) |
| QGradientStops | stops() const |
| CoordinateMode | coordinateMode() const |
| void | setCoordinateMode(CoordinateMode mode) |
| bool | operator==(const QGradient &other) const |
| bool | operator!=(const QGradient &other) const |
Detailed Description
A gradient goes through colors at stops from 0.0 to 1.0. setSpread() decides what lies beyond them. QLinearGradient, QRadialGradient and QConicalGradient, from left to right:

const qreal w = size.width() / 3, h = size.height();
QLinearGradient linear(0, 0, w, h);
linear.setColorAt(0, QColor(40, 90, 200));
linear.setColorAt(1, QColor(230, 240, 255));
painter.fillRect(QRectF(0, 0, w, h), linear);
QRadialGradient radial(QPointF(w * 1.5, h / 2), w / 2, QPointF(w * 1.35, h * 0.35));
radial.setColorAt(0, Qt::white);
radial.setColorAt(1, QColor(200, 40, 60));
painter.fillRect(QRectF(w, 0, w, h), radial);
QConicalGradient conical(QPointF(w * 2.5, h / 2), phase);
for (int i = 0; i <= 6; ++i)
conical.setColorAt(i / 6.0, QColor::fromHsv((i * 60) % 360, 220, 240));
painter.fillRect(QRectF(2 * w, 0, w, h), conical);
A shorter example:
QLinearGradient sky(0, 0, 0, 200);
sky.setColorAt(0.0, QColor(30, 60, 140));
sky.setColorAt(1.0, QColor(200, 220, 255));
painter.fillRect(QRectF(0, 0, 300, 200), sky);
See also QLinearGradient, QRadialGradient, QConicalGradient and QBrush.
Differences from Qt
The coordinate modes are kept, but gradients are always in the logical coordinates of the painter, as with LogicalMode. There are no presets and no interpolation modes.
Member Type Documentation
enum QGradient::CoordinateMode
| Constant | Description |
|---|---|
QGradient::LogicalMode | The points of the gradient are in the logical coordinates of the painter. |
QGradient::StretchToDeviceMode | Relative to the paint device; drawn as LogicalMode in Nt. |
QGradient::ObjectBoundingMode | Relative to the shape filled; drawn as LogicalMode in Nt. |
QGradient::ObjectMode | Relative to the shape filled; drawn as LogicalMode in Nt. |
enum QGradient::Spread
What lies beyond the first and last stops.
| Constant | Description |
|---|---|
QGradient::PadSpread | The colors of the first and last stops. |
QGradient::ReflectSpread | The gradient again, mirrored. |
QGradient::RepeatSpread | The gradient again. |
enum QGradient::Type
| Constant | Description |
|---|---|
QGradient::LinearGradient | A QLinearGradient. |
QGradient::RadialGradient | A QRadialGradient. |
QGradient::ConicalGradient | A QConicalGradient. |
QGradient::NoGradient | No gradient. |
Member Function Documentation
QGradient::QGradient()
Constructs an empty gradient of no type.
CoordinateMode QGradient::coordinateMode() const
Returns the coordinate mode; LogicalMode by default.
bool QGradient::operator!=(const QGradient &other) const
Returns true if other differs from this gradient.
bool QGradient::operator==(const QGradient &other) const
Returns true if other has the same type, stops, spread, mode and points; otherwise returns false.
void QGradient::setColorAt(qreal position, const QColor &color)
Puts color at position, from 0.0 to 1.0, replacing a stop at the same position.
void QGradient::setCoordinateMode(CoordinateMode mode)
Sets the coordinate mode to mode.
void QGradient::setSpread(Spread spread)
Sets what lies beyond the stops to spread.
void QGradient::setStops(const QGradientStops &stops)
Replaces the stops with stops; positions outside 0.0 to 1.0 are skipped.
Spread QGradient::spread() const
Returns what lies beyond the stops; PadSpread by default.
QGradientStops QGradient::stops() const
Returns the stops, in order. A gradient without stops goes from black to white.
Type QGradient::type() const
Returns the type of the gradient.