一、线性渐变
Q_UNUSED(event);
QPainter painter;
painter.begin(this);
QLinearGradient ling(QPoint(0,0),QPoint(400,400));//参数1指定起始点,参数2指定结束点
ling.setColorAt(0,Qt::blue);//指定起点颜色
ling.setColorAt(1,Qt::green);//指定终点颜色
ling.setSpread(QGradient::PadSpread);//设置起点和终点之外的显示模式
QBrush brush(ling);
painter.setBrush(brush);
painter.drawRect(0,0,400,400);
painter.end();
效果:
二、辐射渐变
Q_UNUSED(event);
QPainter painter;
painter.begin(this);
QRadialGradient radg(200,200,100,220,220);//参数(1,2)是辐射中点,参数3是辐射半径,参数(4,5)为渐变起点
radg.setSpread(QGradient::PadSpread);
radg.setColorAt(0,Qt::black);
radg.setColorAt(1,Qt::white);
QBrush brush(radg);
painter.setBrush(brush);
painter.drawRect(0,0,400,400);
painter.end();
效果:
三、角度渐变
Q_UNUSED(event);
QPainter painter;
painter.begin(this);
QConicalGradient cong(200,200,45.0);
cong.setColorAt(0,Qt::black);
cong.setColorAt(1,Qt::white);
QBrush brush(cong);
painter.setBrush(brush);
painter.drawRect(0,0,400,400);
painter.end();
效果: