话不多说,直接上码!
void Widget::paintEvent(QPaintEvent *e)
{
QBrush brush(QColor(0,0,255));
QFont font;
font.setFamily("Microsoft YaHei");
font.setPointSize(20);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QColor(0, 0, 0));//black
//写文字
//painter.drawText(rect, Qt::AlignCenter, QString("Qt by\nThe Qt Company"));
painter.drawText(QPoint(10,30), QString("The Text"));
//画线
painter.drawLine(QPointF(10,35),QPointF(100,35));
//画矩形
painter.save();
painter.setPen(QColor(255, 0, 0));//red
painter.drawRect(QRect(10,40,100,25));
//画圆角矩形
painter.restore();
painter.save();
painter.setBrush(brush);
painter.drawRoundedRect(10,70,100,25,5,5);
painter.restore();
//画弧线
painter.drawArc(15,100,80,80,0,16*90);//90度
//画整圆
painter.drawArc(15,120,80,80,0,5760);
//画扇形
painter.drawPie ( 15, 200, 80, 80, 0, 16*180 );//180度
//画椭圆
painter.drawEllipse(15,250,100,40);
//画多边形
static const QPointF points[4] = {QPointF(20, 290), QPointF(50, 400), QPointF(140, 410), QPointF(200, 350)};
painter.drawPolygon(points, 4);
//---渐变线段
QLinearGradient linearGradient(60, 50,200, 200);
linearGradient.setColorAt(0.2,Qt::red);
linearGradient.setColorAt(0.6,Qt::green);
linearGradient.setColorAt(1.0,Qt::black);
painter.setPen(QPen(QBrush(linearGradient),5));
painter.drawLine(150, 20, 350, 20);
//---渐变文字
QLinearGradient linear;
linear.setStart(150,50);
linear.setFinalStop(150,130);
linear.setColorAt(0,Qt::red);
linear.setColorAt(0.5,Qt::green);
linear.setColorAt(0.8,Qt::black);
linear.setColorAt(1.0,Qt::yellow);
painter.setFont(font);
painter.setPen(QPen(QBrush(linear),5));
painter.drawText(150,50,200,80,Qt::AlignLeft, QString("ABCDEFGHIJK") );
//---渐变矩形
QLinearGradient linear2(QPointF(150, 100), QPointF(210, 180));//设置渐变的起始和结束点,也可做设置渐变方向
linear2.setColorAt(0, Qt::black);
linear2.setColorAt(1, Qt::white);
linear2.setSpread(QGradient::PadSpread);
painter.setPen(QPen(QColor(0, 160, 230), 2));
painter.setBrush(linear2);
painter.drawRect(QRect(150, 100, 60, 60));
//卡拉OK歌词效果
if(paintWith>0){
painter.setPen(QColor(0, 0, 0));//black
painter.drawText(150,50,paintWith,80,Qt::AlignLeft, QString("ABCDEFGHIJK") );
}
this->update();
}