QT中的渐变填充类QGradient

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);
}

效果图:
在这里插入图片描述

  目前只是知识对渐变知识的总结,至于实践应用到程序中,

还没用过,如果大家对渐变有其他的认识或者使用心得,可以留

言分享!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值