QGradient类派生出了3个渐变填充的类
1) 辐射渐变–QRadialGradient
void myWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
int W = this->width();
int H = this->height();
QRadialGradient radialGrad(W/2,H/2,qMax(W/8,H/8),W/2,H/2);
//辐射填充的中心点:W/2,H/2
//辐射填充的半径:qMax(W/8,H/8)
//焦点W/2,H/2
radialGrad.setColorAt(0,Qt::green);//设置起点颜色,0代表起点
radialGrad.setColorAt(1,Qt::blue);//设置终点颜色,1代表终点
radialGrad.setSpread(QGradient::Spread(0));//设置渐变效果(0,1,2)
painter.setBrush(radialGrad);
painter.drawRect(this->rect());
}
效果图:
2) 线性渐变–QLinearGradient
void myWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
int W = this->width();
int H = this->height();
QRect rect(0,0,W,H);
//对角渐变
//QLinearGradient lineGradient(rect.left(),rect.top(),rect.right(),rect.bottom());
//水平渐变
// QLinearGradient lineGradient(rect.left(),rect.top(),rect.right(),rect.top());
//垂直渐变
QLinearGradient lineGradient(rect.left(),rect.top(),rect.left(),rect.bottom());
lineGradient.setColorAt(0,Qt::blue);
lineGradient.setColorAt(0.5,Qt::green);
lineGradient.setColorAt(1,Qt::red);
painter.setBrush(lineGradient);
painter.drawRect(rect);
}
效果图:
3) 圆锥渐变–QConicalGradient
void myWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
int W = this->width();
int H = this->height();
QRect rect(0,0,W,H);
QConicalGradient coniGrad(W/2,H/2,30);
//中心点坐标:W/2,H/2
//起始角度:45
coniGrad.setColorAt(0,Qt::yellow);
coniGrad.setColorAt(0.5,Qt::green);
coniGrad.setColorAt(1,Qt::red);
painter.setBrush(coniGrad);
painter.drawRect(rect);
}
效果图:
目前只是知识对渐变知识的总结,至于实践应用到程序中,
还没用过,如果大家对渐变有其他的认识或者使用心得,可以留
言分享!