QT绘制多边形、渐变线段、文字、带线段的箭头

QT绘制多边形

voidTemp::paintEvent(QPaintEvent *event)

{

       QPainter painter(this);

       painter.setRenderHint(QPainter::Antialiasing,true);

 

QPolygonF m_polygonfLeft;

m_polygonfLeft<

m_polygonfRight

<

 

painter.setPen(QPen(Qt::white,1,Qt::SolidLine,Qt::RoundCap));

painter.setBrush(QBrush(QColor(208,105,38,120),Qt::SolidPattern));

painter.drawPolygon(m_polygonfLeft,Qt::WindingFill);

 

怎么让线段也是渐变的呢?

要知道,直线是用画笔绘制的。这里,如果你仔细查阅了API文档就会发现,QPen是接受QBrush作为参数的。也就是说,你可以利用一个QBrush创建一个QPen,这样,QBrush所有的填充效果都可以用在画笔上了!

voidPaintedWidget::paintEvent(QPaintEvent *event)

{

QPainter painter(this);

painter.setRenderHint(QPainter::Antialiasing,true);

 

QLinearGradient linearGradient(60, 50,200, 200);

linearGradient.setColorAt(0.2,Qt::white);

linearGradient.setColorAt(0.6,Qt::green);

linearGradient.setColorAt(1.0,Qt::black);

 

painter.setPen(QPen(QBrush(linearGradient),5));

painter.drawLine(50, 50, 200, 200);

}


绘制文字

voidPaintedWidget::paintEvent(QPaintEvent *event)

{

QPainter painter(this);

painter.setRenderHint(QPainter::Antialiasing,true);

 

QPoint pointText = QPoint(280,188);

QFont qFont;

qFont.setFamily("微软雅黑");

qFont.setPointSize(18);

qFont.setBold(true);            

painter.setFont(qFont);

 

QString qstrEleInfo = WallE;

painter.drawText(pointText,qstrEleInfo);

 

绘制带线段的箭头

voidPaintedWidget::paintEvent(QPaintEvent *event)

{

       QPainter painter(this);

       painter.setRenderHint(QPainter::Antialiasing,true);

 

QPoint pointleft = QPoint(280,188);

QPoint pointright = QPoint(470,188);

QPointF pointTriangle1(pointleft.x(),pointright.y());

QPointF pointTriangle2(pointleft.x() + 10,pointright.y() -3);

QPointF pointTriangle3(pointleft.x() + 10,pointright.y() +3);

 

QPolygonF polygonfTriangle;

polygonfTriangle<

painter.setBrush(QBrush(QColor(0,150,250),Qt::SolidPattern));

painter.setPen(QPen(QColor(0,150,250),1,Qt::SolidLine,Qt::RoundCap));

painter.drawPolygon(polygonfTriangle,Qt::WindingFill);

 

painter.setPen(QPen(QColor(0,150,250),1,Qt::SolidLine,Qt::RoundCap));

painter.drawLine(pointleft,pointright);

你可能感兴趣的:(QT绘制图形)