6.0.0-rc4Source

QGradient Class

The QGradient class is the base of the gradients that brushes fill with. More…

Header#include <QGradient>
CMaketarget_link_libraries(app PRIVATE Nt::Gui)
ModuleNt GUI C++ Classes
Inherited byQConicalGradient, QLinearGradient, QRadialGradient

List of all members, including inherited members

Public Types

enumType
enumSpread
enumCoordinateMode

Public Functions

QGradient()
Typetype() const
voidsetSpread(Spread spread)
Spreadspread() const
voidsetColorAt(qreal position, const QColor &color)
voidsetStops(const QGradientStops &stops)
QGradientStopsstops() const
CoordinateModecoordinateMode() const
voidsetCoordinateMode(CoordinateMode mode)
booloperator==(const QGradient &other) const
booloperator!=(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:

A linear, a radial and a conical gradient

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

ConstantDescription
QGradient::LogicalModeThe points of the gradient are in the logical coordinates of the painter.
QGradient::StretchToDeviceModeRelative to the paint device; drawn as LogicalMode in Nt.
QGradient::ObjectBoundingModeRelative to the shape filled; drawn as LogicalMode in Nt.
QGradient::ObjectModeRelative to the shape filled; drawn as LogicalMode in Nt.

enum QGradient::Spread

What lies beyond the first and last stops.

ConstantDescription
QGradient::PadSpreadThe colors of the first and last stops.
QGradient::ReflectSpreadThe gradient again, mirrored.
QGradient::RepeatSpreadThe gradient again.

enum QGradient::Type

ConstantDescription
QGradient::LinearGradientA QLinearGradient.
QGradient::RadialGradientA QRadialGradient.
QGradient::ConicalGradientA QConicalGradient.
QGradient::NoGradientNo 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.