本页内容

QGradient Class

QGradient 类需与QBrush 结合使用,以指定渐变填充。更多内容...

头文件: #include <QGradient>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承自:

QConicalGradient、QLinearGradient 以及QRadialGradient

公共类型

enum CoordinateMode { LogicalMode, ObjectMode, StretchToDeviceMode, ObjectBoundingMode }
enum Preset { WarmFlame, NightFade, SpringWarmth, JuicyPeach, YoungPassion, …, PerfectBlue }
enum Spread { PadSpread, RepeatSpread, ReflectSpread }
enum Type { LinearGradient, RadialGradient, ConicalGradient, NoGradient }

公共函数

QGradient(QGradient::Preset preset)
QGradient::CoordinateMode coordinateMode() const
void setColorAt(qreal position, const QColor &color)
void setCoordinateMode(QGradient::CoordinateMode mode)
void setSpread(QGradient::Spread method)
void setStops(const QGradientStops &stopPoints)
QGradient::Spread spread() const
QGradientStops stops() const
QGradient::Type type() const
bool operator!=(const QGradient &gradient) const
bool operator==(const QGradient &gradient) const

详细说明

Qt 目前支持三种渐变填充类型:

  • 线性渐变在起始点和终点之间进行颜色插值。
  • 简单径向渐变会在焦点与围绕该焦点的圆上的端点之间进行颜色插值。
  • 扩展径向渐变在中心点与焦点圆之间进行颜色插值。
  • 锥形渐变在中心点周围对颜色进行插值。

可通过调用type()函数获取渐变的类型。每种类型都由QGradient的子类表示:

渐变中的颜色通过QGradientStop 类型的停点来定义;即一个位置和一种颜色。使用setColorAt()函数定义单个停点。或者,使用setStops()函数一次性定义多个停点。请注意,后者会替换当前的停点集。

正是渐变的完整停点集(可通过stops() 函数访问)描述了渐变区域应如何填充。如果未指定任何停点,则使用从 0 处的黑色到 1 处的白色的渐变。

一个从 (100, 100) 处的黑色到 (200, 200) 处的白色、呈对角线的线性渐变,可以这样指定:

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

渐变可以包含任意数量的停点。以下代码将创建一个从中心红色开始、边缘依次为蓝色和绿色的径向渐变:

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

通过使用setSpread()函数指定spread method ,可以使渐变在区域外重复或镜像。默认情况下,外围区域将填充为最近停靠点的颜色。当前设置的spread method 可通过spread()函数获取。QGradient::Spread 枚举定义了三种不同的方法:

请注意,setSpread() 函数仅对线性渐变和径向渐变有效。原因是锥形渐变按定义是闭合的,即锥形渐变填充了 0 到 360 度的整个圆周,而径向或线性渐变的边界则可分别通过其半径或最终停靠点来指定。

渐变坐标可以以逻辑坐标、相对于设备坐标或相对于对象边界框坐标的方式指定。可通过setCoordinateMode()函数设置coordinate mode 。默认值为LogicalMode ,即渐变坐标的指定方式与对象坐标相同。要获取当前设置的coordinate mode ,请使用coordinateMode()。

另请参阅 《梯度示例》和QBrush 。

成员类型文档

enum QGradient::CoordinateMode

此枚举指定了渐变坐标如何映射到使用该渐变的绘图设备上。

常量值描述
QGradient::LogicalMode0这是默认模式。渐变坐标与对象坐标一样,是在逻辑空间中指定的。
QGradient::ObjectMode3在此模式下,渐变坐标相对于正在绘制的对象的边界矩形,其中 (0,0) 位于对象边界矩形的左上角,(1,1) 位于对象边界矩形的右下角。此值是在 Qt 5.12 中添加的。
QGradient::StretchToDeviceMode1在此模式下,渐变坐标相对于绘图设备的边界矩形,其中 (0,0) 位于绘图设备左上角,(1,1) 位于绘图设备右下角。
QGradient::ObjectBoundingMode2此模式与 ObjectMode 相同,区别在于,若有 {QBrush::transform()} {brush transform},则其应用是相对于逻辑空间而非对象空间。此枚举值已弃用,不应在新代码中使用。

enum QGradient::Preset

该枚举为QGradient 定义了一组预设,基于https://webgradients.com/中的渐变效果。

常量值
QGradient::WarmFlame1
QGradient::NightFade2
QGradient::SpringWarmth3
QGradient::JuicyPeach4
QGradient::YoungPassion5
QGradient::LadyLips6
QGradient::SunnyMorning7
QGradient::RainyAshville8
QGradient::FrozenDreams9
QGradient::WinterNeva10
QGradient::DustyGrass11
QGradient::TemptingAzure12
QGradient::HeavyRain13
QGradient::AmyCrisp14
QGradient::MeanFruit15
QGradient::DeepBlue16
QGradient::RipeMalinka17
QGradient::CloudyKnoxville18
QGradient::MalibuBeach19
QGradient::NewLife20
QGradient::TrueSunset21
QGradient::MorpheusDen22
QGradient::RareWind23
QGradient::NearMoon24
QGradient::WildApple25
QGradient::SaintPetersburg26
QGradient::PlumPlate28
QGradient::EverlastingSky29
QGradient::HappyFisher30
QGradient::Blessing31
QGradient::SharpeyeEagle32
QGradient::LadogaBottom33
QGradient::LemonGate34
QGradient::ItmeoBranding35
QGradient::ZeusMiracle36
QGradient::OldHat37
QGradient::StarWine38
QGradient::HappyAcid41
QGradient::AwesomePine42
QGradient::NewYork43
QGradient::ShyRainbow44
QGradient::MixedHopes46
QGradient::FlyHigh47
QGradient::StrongBliss48
QGradient::FreshMilk49
QGradient::SnowAgain50
QGradient::FebruaryInk51
QGradient::KindSteel52
QGradient::SoftGrass53
QGradient::GrownEarly54
QGradient::SharpBlues55
QGradient::ShadyWater56
QGradient::DirtyBeauty57
QGradient::GreatWhale58
QGradient::TeenNotebook59
QGradient::PoliteRumors60
QGradient::SweetPeriod61
QGradient::WideMatrix62
QGradient::SoftCherish63
QGradient::RedSalvation64
QGradient::BurningSpring65
QGradient::NightParty66
QGradient::SkyGlider67
QGradient::HeavenPeach68
QGradient::PurpleDivision69
QGradient::AquaSplash70
QGradient::SpikyNaga72
QGradient::LoveKiss73
QGradient::CleanMirror75
QGradient::PremiumDark76
QGradient::ColdEvening77
QGradient::CochitiLake78
QGradient::SummerGames79
QGradient::PassionateBed80
QGradient::MountainRock81
QGradient::DesertHump82
QGradient::JungleDay83
QGradient::PhoenixStart84
QGradient::OctoberSilence85
QGradient::FarawayRiver86
QGradient::AlchemistLab87
QGradient::OverSun88
QGradient::PremiumWhite89
QGradient::MarsParty90
QGradient::EternalConstance91
QGradient::JapanBlush92
QGradient::SmilingRain93
QGradient::CloudyApple94
QGradient::BigMango95
QGradient::HealthyWater96
QGradient::AmourAmour97
QGradient::RiskyConcrete98
QGradient::StrongStick99
QGradient::ViciousStance100
QGradient::PaloAlto101
QGradient::HappyMemories102
QGradient::MidnightBloom103
QGradient::Crystalline104
QGradient::PartyBliss106
QGradient::ConfidentCloud107
QGradient::LeCocktail108
QGradient::RiverCity109
QGradient::FrozenBerry110
QGradient::ChildCare112
QGradient::FlyingLemon113
QGradient::NewRetrowave114
QGradient::HiddenJaguar115
QGradient::AboveTheSky116
QGradient::Nega117
QGradient::DenseWater118
QGradient::Seashore120
QGradient::MarbleWall121
QGradient::CheerfulCaramel122
QGradient::NightSky123
QGradient::MagicLake124
QGradient::YoungGrass125
QGradient::ColorfulPeach126
QGradient::GentleCare127
QGradient::PlumBath128
QGradient::HappyUnicorn129
QGradient::AfricanField131
QGradient::SolidStone132
QGradient::OrangeJuice133
QGradient::GlassWater134
QGradient::NorthMiracle136
QGradient::FruitBlend137
QGradient::MillenniumPine138
QGradient::HighFlight139
QGradient::MoleHall140
QGradient::SpaceShift142
QGradient::ForestInei143
QGradient::RoyalGarden144
QGradient::RichMetal145
QGradient::JuicyCake146
QGradient::SmartIndigo147
QGradient::SandStrike148
QGradient::NorseBeauty149
QGradient::AquaGuidance150
QGradient::SunVeggie151
QGradient::SeaLord152
QGradient::BlackSea153
QGradient::GrassShampoo154
QGradient::LandingAircraft155
QGradient::WitchDance156
QGradient::SleeplessNight157
QGradient::AngelCare158
QGradient::CrystalRiver159
QGradient::SoftLipstick160
QGradient::SaltMountain161
QGradient::PerfectWhite162
QGradient::FreshOasis163
QGradient::StrictNovember164
QGradient::MorningSalad165
QGradient::DeepRelief166
QGradient::SeaStrike167
QGradient::NightCall168
QGradient::SupremeSky169
QGradient::LightBlue170
QGradient::MindCrawl171
QGradient::LilyMeadow172
QGradient::SugarLollipop173
QGradient::SweetDessert174
QGradient::MagicRay175
QGradient::TeenParty176
QGradient::FrozenHeat177
QGradient::GagarinView178
QGradient::FabledSunset179
QGradient::PerfectBlue180

enum QGradient::Spread

指定渐变区域外的区域应如何填充。

常量值描述
QGradient::PadSpread0该区域将填充为最接近的色停颜色。这是默认设置。
QGradient::RepeatSpread2渐变将在渐变区域之外重复。
QGradient::ReflectSpread1渐变在渐变区域外进行镜像。

另请参阅 spread() 和setSpread()。

enum QGradient::Type

指定渐变的类型。

常量值描述
QGradient::LinearGradient0对起始点和终点之间的颜色进行插值(QLinearGradient )。
QGradient::RadialGradient1对焦点及其周围圆上的端点之间的颜色进行插值(QRadialGradient )。
QGradient::ConicalGradient2对中心点周围的颜色进行插值(QConicalGradient )。
QGradient::NoGradient3不使用渐变。

另请参阅 type()。

成员函数文档

QGradient::QGradient(QGradient::Preset preset)

根据预定义的preset 生成一个渐变。

生成的渐变的坐标模式为QGradient::ObjectMode ,因此该预设可应用于任意大小的对象。

QGradient::CoordinateMode QGradient::coordinateMode() const

返回此渐变的坐标模式。默认模式为LogicalMode 。

另请参阅 setCoordinateMode()。

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

在指定的position 处,使用指定的color 创建一个停点。指定的position 必须在0到1之间。

另请参阅 setStops() 和stops()。

void QGradient::setCoordinateMode(QGradient::CoordinateMode mode)

将此渐变的坐标模式设置为mode 。默认模式为LogicalMode 。

另请参阅 coordinateMode()。

void QGradient::setSpread(QGradient::Spread method)

指定应用于此渐变的扩散method 。

请注意,此函数仅对线性渐变和径向渐变有效。

另请参阅 spread()。

void QGradient::setStops(const QGradientStops &stopPoints)

将当前的一组停点替换为给定的stopPoints 。各点的位置必须在0到1的范围内,并且必须按从小到大的顺序排序。

另请参阅 setColorAt() 和stops()。

QGradient::Spread QGradient::spread() const

返回该渐变所使用的扩展方法。默认值为PadSpread 。

另请参阅 setSpread()。

QGradientStops QGradient::stops() const

返回此渐变的停点。

如果未指定任何停点,则使用从 0 处的黑色到 1 处的白色的渐变。

另请参阅 setStops() 和setColorAt()。

QGradient::Type QGradient::type() const

返回渐变的类型。

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

如果该梯度与其他指定的gradient 相同,则返回true ;否则返回false 。

另请参阅 operator==()。

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

如果该梯度与指定的其他gradient 相同,则返回true ;否则返回false 。

另请参阅 operator!=()。

相关的非成员

QGradientStop

std::pair<qreal,QColor> 的 typedef。

QGradientStops

QList 的typedef定义 <QGradientStop>。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.