Qt 4.8
Public Types | Public Functions | Properties | Friends | Related Functions | List of all members
QGradient Class Reference

The QGradient class is used in combination with QBrush to specify gradient fills. More...

#include <qbrush.h>

Inheritance diagram for QGradient:
QConicalGradient QLinearGradient QRadialGradient

Public Types

enum  CoordinateMode { LogicalMode, StretchToDeviceMode, ObjectBoundingMode }
 This enum specifies how gradient coordinates map to the paint device on which the gradient is used. More...
 
enum  InterpolationMode { ColorInterpolation, ComponentInterpolation }
 
enum  Spread { PadSpread, ReflectSpread, RepeatSpread }
 Specifies how the area outside the gradient area should be filled. More...
 
enum  Type { LinearGradient, RadialGradient, ConicalGradient, NoGradient }
 Specifies the type of gradient. More...
 

Public Functions

CoordinateMode coordinateMode () const
 Returns the coordinate mode of this gradient. More...
 
InterpolationMode interpolationMode () const
 Returns the interpolation mode of this gradient. More...
 
bool operator!= (const QGradient &other) const
 Returns true if the gradient is the same as the other gradient specified; otherwise returns false. More...
 
bool operator== (const QGradient &gradient) const
 Returns true if the gradient is the same as the other gradient specified; otherwise returns false. More...
 
bool operator== (const QGradient &gradient)
 
 QGradient ()
 
void setColorAt (qreal pos, const QColor &color)
 Creates a stop point at the given position with the given color. More...
 
void setCoordinateMode (CoordinateMode mode)
 Sets the coordinate mode of this gradient to mode. More...
 
void setInterpolationMode (InterpolationMode mode)
 Sets the interpolation mode of this gradient to mode. More...
 
void setSpread (Spread spread)
 Specifies the spread method that should be used for this gradient. More...
 
void setStops (const QGradientStops &stops)
 Replaces the current set of stop points with the given stopPoints. More...
 
Spread spread () const
 Returns the spread method use by this gradient. More...
 
QGradientStops stops () const
 Returns the stop points for this gradient. More...
 
Type type () const
 Returns the type of gradient. More...
 

Properties

void * dummy
 
union {
   struct {
      qreal   angle
 
      qreal   cx
 
      qreal   cy
 
   }   conical
 
   struct {
      qreal   x1
 
      qreal   x2
 
      qreal   y1
 
      qreal   y2
 
   }   linear
 
   struct {
      qreal   cradius
 
      qreal   cx
 
      qreal   cy
 
      qreal   fx
 
      qreal   fy
 
   }   radial
 
m_data
 
Spread m_spread
 
QGradientStops m_stops
 
Type m_type
 

Friends

class QBrush
 
class QConicalGradient
 
class QLinearGradient
 
class QRadialGradient
 

Related Functions

(Note that these are not member functions.)

 QGradientStop
 Typedef for QPair<qreal , QColor>. More...
 
 QGradientStops
 Typedef for QVector<QGradientStop>. More...
 

Detailed Description

The QGradient class is used in combination with QBrush to specify gradient fills.

Qt currently supports three types of gradient fills:

A gradient's type can be retrieved using the type() function. Each of the types is represented by a subclass of QGradient:

QLinearGradient QRadialGradient QConicalGradient
qgradient-linear.png
qgradient-radial.png
qgradient-conical.png

The colors in a gradient are defined using stop points of the QGradientStop type; i.e., a position and a color. Use the setColorAt() function to define a single stop point. Alternatively, use the setStops() function to define several stop points in one go. Note that the latter function replaces the current set of stop points.

It is the gradient's complete set of stop points (accessible through the stops() function) that describes how the gradient area should be filled. If no stop points have been specified, a gradient of black at 0 to white at 1 is used.

A diagonal linear gradient from black at (100, 100) to white at (200, 200) could be specified like this:

QLinearGradient linearGrad(QPointF(100, 100), QPointF(200, 200));
linearGrad.setColorAt(0, Qt::black);
linearGrad.setColorAt(1, Qt::white);

A gradient can have an arbitrary number of stop points. The following would create a radial gradient starting with red in the center, blue and then green on the edges:

QRadialGradient radialGrad(QPointF(100, 100), 100);
radialGrad.setColorAt(0, Qt::red);
radialGrad.setColorAt(0.5, Qt::blue);
radialGrad.setColorAt(1, Qt::green);

It is possible to repeat or reflect the gradient outside its area by specifiying the QGradient::Spread{spread method} using the setSpread() function. The default is to pad the outside area with the color at the closest stop point. The currently set QGradient::Spread can be retrieved using the spread() function. The QGradient::Spread enum defines three different methods:

qradialgradient-pad.png
qradialgradient-repeat.png
qradialgradient-reflect.png
QGradient::PadSpread{PadSpread} QGradient::RepeatSpread{RepeatSpread} QGradient::ReflectSpread{ReflectSpread}

Note that the setSpread() function only has effect for linear and radial gradients. The reason is that the conical gradient is closed by definition, i.e. the conical gradient fills the entire circle from 0 - 360 degrees, while the boundary of a radial or a linear gradient can be specified through its radius or final stop points, respectively.

The gradient coordinates can be specified in logical coordinates, relative to device coordinates, or relative to object bounding box coordinates. The QGradient::CoordinateMode{coordinate mode} can be set using the setCoordinateMode() function. The default is LogicalMode, where the gradient coordinates are specified in the same way as the object coordinates. To retrieve the currently set QGradient::CoordinateMode {coordinate mode} use coordinateMode().

See also
{demos/gradients}{The Gradients Demo}, QBrush

Definition at line 201 of file qbrush.h.

Enumerations

◆ CoordinateMode

This enum specifies how gradient coordinates map to the paint device on which the gradient is used.

Since
4.4
  • LogicalMode This is the default mode. The gradient coordinates are specified logical space just like the object coordinates.
  • StretchToDeviceMode In this mode the gradient coordinates are relative to the bounding rectangle of the paint device, with (0,0) in the top left corner, and (1,1) in the bottom right corner of the paint device.
  • ObjectBoundingMode In this mode the gradient coordinates are relative to the bounding rectangle of the object being drawn, with (0,0) in the top left corner, and (1,1) in the bottom right corner of the object's bounding rectangle.
Enumerator
LogicalMode 
StretchToDeviceMode 
ObjectBoundingMode 

Definition at line 219 of file qbrush.h.

◆ InterpolationMode

Since
4.5
Warning
This function is not part of the public interface.
  • ComponentInterpolation The color components and the alpha component are independently linearly interpolated.
  • ColorInterpolation The colors are linearly interpolated in premultiplied color space.
Enumerator
ColorInterpolation 
ComponentInterpolation 

Definition at line 225 of file qbrush.h.

◆ Spread

Specifies how the area outside the gradient area should be filled.

  • PadSpread The area is filled with the closest stop color. This is the default.
  • RepeatSpread The gradient is repeated outside the gradient area.
  • ReflectSpread The gradient is reflected outside the gradient area.
See also
spread(), setSpread()
Enumerator
PadSpread 
ReflectSpread 
RepeatSpread 

Definition at line 213 of file qbrush.h.

◆ Type

Specifies the type of gradient.

  • LinearGradient Interpolates colors between start and end points (QLinearGradient).
  • RadialGradient Interpolate colors between a focal point and end points on a circle surrounding it (QRadialGradient).
  • ConicalGradient Interpolate colors around a center point (QConicalGradient).
  • NoGradient No gradient is used.
See also
type()
Enumerator
LinearGradient 
RadialGradient 
ConicalGradient 
NoGradient 

Definition at line 206 of file qbrush.h.

Constructors and Destructors

◆ QGradient()

QGradient::QGradient ( )
Warning
This function is not part of the public interface.

Definition at line 1380 of file qbrush.cpp.

1381  : m_type(NoGradient), dummy(0)
1382 {
1383 }
Type m_type
Definition: qbrush.h:260
void * dummy
Definition: qbrush.h:274

Functions

◆ coordinateMode()

QGradient::CoordinateMode QGradient::coordinateMode ( ) const

Returns the coordinate mode of this gradient.

Since
4.4

The default mode is LogicalMode.

Definition at line 1563 of file qbrush.cpp.

Referenced by QPainterPrivate::checkEmulation(), coordinateMode(), QEmulationPaintEngine::drawTextItem(), QEmulationPaintEngine::fill(), QPainter::fillPath(), QPainter::fillRect(), fillRegion(), needsResolving(), operator<<(), QSvgPaintEngine::saveGradientUnits(), QEmulationPaintEngine::stroke(), and QPainter::strokePath().

1564 {
1566  return CoordinateMode(i & 0x03);
1567 }
#define Q_DUMMY_ACCESSOR
Definition: qbrush.cpp:1530
CoordinateMode
This enum specifies how gradient coordinates map to the paint device on which the gradient is used...
Definition: qbrush.h:219

◆ interpolationMode()

QGradient::InterpolationMode QGradient::interpolationMode ( ) const

Returns the interpolation mode of this gradient.

Since
4.5
Warning
This function is not part of the public interface.

The default mode is ColorInterpolation.

Definition at line 1607 of file qbrush.cpp.

Referenced by QGL2GradientCache::addCacheElement(), QGLGradientCache::addCacheElement(), QGradientCache::addCacheElement(), QGL2GradientCache::generateGradientColorTable(), QGLGradientCache::generateGradientColorTable(), QGradientCache::generateGradientColorTable(), QGL2GradientCache::getBuffer(), QGLGradientCache::getBuffer(), QGradientCache::getBuffer(), operator<<(), and QSvgPaintEngine::saveGradientStops().

1608 {
1610  return InterpolationMode((i >> 2) & 0x01);
1611 }
#define Q_DUMMY_ACCESSOR
Definition: qbrush.cpp:1530
InterpolationMode
Definition: qbrush.h:225

◆ operator!=()

bool QGradient::operator!= ( const QGradient gradient) const
inline

Returns true if the gradient is the same as the other gradient specified; otherwise returns false.

Since
4.2
See also
operator==()

Definition at line 249 of file qbrush.h.

250  { return !operator==(other); }
bool operator==(const QGradient &gradient) const
Returns true if the gradient is the same as the other gradient specified; otherwise returns false...
Definition: qbrush.cpp:1650

◆ operator==() [1/2]

bool QGradient::operator== ( const QGradient gradient) const

Returns true if the gradient is the same as the other gradient specified; otherwise returns false.

See also
operator!=()

Definition at line 1650 of file qbrush.cpp.

1651 {
1652  if (gradient.m_type != m_type
1653  || gradient.m_spread != m_spread
1654  || gradient.dummy != dummy) return false;
1655 
1656  if (m_type == LinearGradient) {
1657  if (m_data.linear.x1 != gradient.m_data.linear.x1
1658  || m_data.linear.y1 != gradient.m_data.linear.y1
1659  || m_data.linear.x2 != gradient.m_data.linear.x2
1660  || m_data.linear.y2 != gradient.m_data.linear.y2)
1661  return false;
1662  } else if (m_type == RadialGradient) {
1663  if (m_data.radial.cx != gradient.m_data.radial.cx
1664  || m_data.radial.cy != gradient.m_data.radial.cy
1665  || m_data.radial.fx != gradient.m_data.radial.fx
1666  || m_data.radial.fy != gradient.m_data.radial.fy
1667  || m_data.radial.cradius != gradient.m_data.radial.cradius)
1668  return false;
1669  } else { // m_type == ConicalGradient
1670  if (m_data.conical.cx != gradient.m_data.conical.cx
1671  || m_data.conical.cy != gradient.m_data.conical.cy
1672  || m_data.conical.angle != gradient.m_data.conical.angle)
1673  return false;
1674  }
1675 
1676  return stops() == gradient.stops();
1677 }
struct QGradient::@216::@218 radial
struct QGradient::@216::@217 linear
Type m_type
Definition: qbrush.h:260
QGradientStops stops() const
Returns the stop points for this gradient.
Definition: qbrush.cpp:1520
union QGradient::@216 m_data
void * dummy
Definition: qbrush.h:274
struct QGradient::@216::@219 conical
Spread m_spread
Definition: qbrush.h:261

◆ operator==() [2/2]

bool QGradient::operator== ( const QGradient gradient)
Warning
This function is not part of the public interface.

Definition at line 1682 of file qbrush.cpp.

1683 {
1684  return const_cast<const QGradient *>(this)->operator==(gradient);
1685 }
The QGradient class is used in combination with QBrush to specify gradient fills. ...
Definition: qbrush.h:201

◆ setColorAt()

void QGradient::setColorAt ( qreal  position,
const QColor color 
)

Creates a stop point at the given position with the given color.

The given position must be in the range 0 to 1.

See also
setStops(), stops()

Definition at line 1475 of file qbrush.cpp.

Referenced by QPlastiqueStyle::drawComplexControl(), QCleanlooksStyle::drawComplexControl(), QWindowsVistaStyle::drawControl(), QCommonStyle::drawControl(), QPlastiqueStyle::drawControl(), QCleanlooksStyle::drawControl(), QMacStyle::drawControl(), QStyleHelper::drawDial(), QWindowsCEStyle::drawPrimitive(), QPlastiqueStyle::drawPrimitive(), QMacStyle::drawPrimitive(), drawTabBase(), drawTabShape(), QScriptEdit::extraAreaPaintEvent(), parseStopNode(), qt_cleanlooks_draw_buttongradient(), qt_cleanlooks_draw_gradient(), qt_cleanlooks_draw_mdibutton(), qt_plastique_draw_gradient(), and setStops().

1476 {
1477  if ((pos > 1 || pos < 0) && !qIsNaN(pos)) {
1478  qWarning("QGradient::setColorAt: Color position must be specified in the range 0 to 1");
1479  return;
1480  }
1481 
1482  int index = 0;
1483  if (!qIsNaN(pos))
1484  while (index < m_stops.size() && m_stops.at(index).first < pos) ++index;
1485 
1486  if (index < m_stops.size() && m_stops.at(index).first == pos)
1487  m_stops[index].second = color;
1488  else
1489  m_stops.insert(index, QGradientStop(pos, color));
1490 }
QGradientStops m_stops
Definition: qbrush.h:262
T1 first
Definition: qpair.h:65
QGradientStop
Typedef for QPair<qreal , QColor>.
Q_CORE_EXPORT void qWarning(const char *,...)
Q_CORE_EXPORT bool qIsNaN(double d)
Returns true if the double {d} is not a number (NaN).
Definition: qnumeric.cpp:55
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
void insert(int i, const T &t)
Inserts value at index position i in the vector.
Definition: qvector.h:362
quint16 index
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ setCoordinateMode()

void QGradient::setCoordinateMode ( CoordinateMode  mode)

Sets the coordinate mode of this gradient to mode.

Since
4.4

The default mode is LogicalMode.

Definition at line 1578 of file qbrush.cpp.

Referenced by QEmulationPaintEngine::drawTextItem(), operator>>(), parseBaseGradient(), and stretchGradientToUserSpace().

1579 {
1581  i &= ~0x03;
1582  i |= uint(mode);
1583  dummy = p;
1584 }
#define Q_DUMMY_ACCESSOR
Definition: qbrush.cpp:1530
unsigned int uint
Definition: qglobal.h:996
void * dummy
Definition: qbrush.h:274

◆ setInterpolationMode()

void QGradient::setInterpolationMode ( InterpolationMode  mode)

Sets the interpolation mode of this gradient to mode.

Since
4.5
Warning
This function is not part of the public interface.

The default mode is ColorInterpolation.

Definition at line 1623 of file qbrush.cpp.

Referenced by createLinearGradientNode(), createRadialGradientNode(), and operator>>().

1624 {
1626  i &= ~(1 << 2);
1627  i |= (uint(mode) << 2);
1628  dummy = p;
1629 }
#define Q_DUMMY_ACCESSOR
Definition: qbrush.cpp:1530
unsigned int uint
Definition: qglobal.h:996
void * dummy
Definition: qbrush.h:274

◆ setSpread()

void QGradient::setSpread ( Spread  method)
inline

Specifies the spread method that should be used for this gradient.

Note that this function only has effect for linear and radial gradients.

See also
spread()

Definition at line 277 of file qbrush.h.

Referenced by operator>>(), and parseBaseGradient().

278 { m_spread = aspread; }
Spread m_spread
Definition: qbrush.h:261

◆ setStops()

void QGradient::setStops ( const QGradientStops stopPoints)

Replaces the current set of stop points with the given stopPoints.

The positions of the points must be in the range 0 to 1, and must be sorted with the lowest point first.

See also
setColorAt(), stops()

Definition at line 1504 of file qbrush.cpp.

Referenced by QSvgGradientStyle::brush(), QCleanlooksStyle::drawComplexControl(), QCleanlooksStyle::drawControl(), operator>>(), parseBaseGradient(), parseStopNode(), qBrushDark(), qBrushLight(), qBrushSetAlphaF(), qMapGradientToRect(), qt_cleanlooks_draw_buttongradient(), and qt_cleanlooks_draw_gradient().

1505 {
1506  m_stops.clear();
1507  for (int i=0; i<stops.size(); ++i)
1509 }
QGradientStops m_stops
Definition: qbrush.h:262
void setColorAt(qreal pos, const QColor &color)
Creates a stop point at the given position with the given color.
Definition: qbrush.cpp:1475
T1 first
Definition: qpair.h:65
T2 second
Definition: qpair.h:66
QGradientStops stops() const
Returns the stop points for this gradient.
Definition: qbrush.cpp:1520
void clear()
Removes all the elements from the vector and releases the memory used by the vector.
Definition: qvector.h:347
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ spread()

QGradient::Spread QGradient::spread ( ) const
inline

◆ stops()

QGradientStops QGradient::stops ( ) const

Returns the stop points for this gradient.

If no stop points have been specified, a gradient of black at 0 to white at 1 is used.

See also
setStops(), setColorAt()

Definition at line 1520 of file qbrush.cpp.

Referenced by QGL2GradientCache::addCacheElement(), QGLGradientCache::addCacheElement(), QGradientCache::addCacheElement(), cgColorForQColor(), QCleanlooksStyle::drawComplexControl(), QCleanlooksStyle::drawControl(), QGL2GradientCache::generateGradientColorTable(), QGLGradientCache::generateGradientColorTable(), QGradientCache::generateGradientColorTable(), QGL2GradientCache::getBuffer(), QGLGradientCache::getBuffer(), QGradientCache::getBuffer(), QBrush::isOpaque(), operator<<(), operator==(), parseBaseGradient(), parseStopNode(), QPdf::patternForBrush(), qMapGradientToRect(), rasterFallbackWarn(), QSvgGradientStyle::resolveStops(), QSvgPaintEngine::saveGradientStops(), QVGPaintEnginePrivate::setupColorRamp(), and QPdfEnginePrivate::~QPdfEnginePrivate().

1521 {
1522  if (m_stops.isEmpty()) {
1523  QGradientStops tmp;
1524  tmp << QGradientStop(0, Qt::black) << QGradientStop(1, Qt::white);
1525  return tmp;
1526  }
1527  return m_stops;
1528 }
QGradientStops m_stops
Definition: qbrush.h:262
QGradientStop
Typedef for QPair<qreal , QColor>.
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139

◆ type()

QGradient::Type QGradient::type ( ) const
inline

Friends and Related Functions

◆ QBrush

friend class QBrush
friend

Definition at line 258 of file qbrush.h.

◆ QConicalGradient

friend class QConicalGradient
friend

Definition at line 257 of file qbrush.h.

◆ QGradientStop()

QGradientStop
related

Typedef for QPair<qreal , QColor>.

Referenced by setColorAt(), and stops().

◆ QGradientStops()

QGradientStops
related

Typedef for QVector<QGradientStop>.

◆ QLinearGradient

friend class QLinearGradient
friend

Definition at line 255 of file qbrush.h.

◆ QRadialGradient

friend class QRadialGradient
friend

Definition at line 256 of file qbrush.h.

Properties

◆ angle

qreal QGradient::angle

Definition at line 271 of file qbrush.h.

Referenced by QConicalGradient::QConicalGradient(), and QConicalGradient::setAngle().

◆ conical

struct { ... } QGradient::conical

Referenced by operator==().

◆ cradius

qreal QGradient::cradius

Definition at line 268 of file qbrush.h.

◆ cx

qreal QGradient::cx

◆ cy

qreal QGradient::cy

◆ dummy

void* QGradient::dummy
private

◆ fx

qreal QGradient::fx

Definition at line 268 of file qbrush.h.

Referenced by QRadialGradient::QRadialGradient().

◆ fy

qreal QGradient::fy

Definition at line 268 of file qbrush.h.

Referenced by QRadialGradient::QRadialGradient().

◆ linear

struct { ... } QGradient::linear

Referenced by operator==().

◆ m_data

union { ... } QGradient::m_data

◆ m_spread

Spread QGradient::m_spread
private

◆ m_stops

QGradientStops QGradient::m_stops
private

Definition at line 262 of file qbrush.h.

Referenced by setColorAt(), setStops(), and stops().

◆ m_type

Type QGradient::m_type
private

◆ radial

struct { ... } QGradient::radial

Referenced by operator==().

◆ x1

qreal QGradient::x1

Definition at line 265 of file qbrush.h.

◆ x2

qreal QGradient::x2

Definition at line 265 of file qbrush.h.

◆ y1

qreal QGradient::y1

Definition at line 265 of file qbrush.h.

◆ y2

qreal QGradient::y2

Definition at line 265 of file qbrush.h.


The documentation for this class was generated from the following files: