QT绘制多边形
voidTemp::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
QPolygonF m_polygonfLeft;
m_polygonfLeft<<QPointF(160.0,190.0)<<QPointF(160.0,210.0)<<QPointF(225.0,188.0)<<QPointF(225.0,165.0);
m_polygonfRight
<<QPointF(225.0,165.0)<<QPointF(225.0,190.0)<<QPointF(305.0,205.0)<<QPointF(305.0,185.0);
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<<pointTriangle1<<pointTriangle2<<pointTriangle3;
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);
}