QT - 图像处理 ( 5 ) - QPainterPath、QPolygonF绘制中空图形( 环、三角板)

1、使用QRegion做出一个圆环区域,使用QPainterPath添加这个区域,然后fillPath。

    QPainter painter(this);
    painter.save();

    //设置反锯齿
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::Qt4CompatiblePainting);

    QRect drawRect = event->rect();
    QRegion region(drawRect.adjusted(10,10,-10,-10),QRegion::Ellipse);
    drawRect.setSize(QSize(drawRect.width()/2,drawRect.height()/2));
    drawRect.moveTopLeft(QPoint((event->rect().width() - drawRect.width())/2,(event->rect().height() - drawRect.height())/2));

    QRegion region2(drawRect,QRegion::Ellipse);
    QPainterPath painterPath;
    painterPath.addRegion(region.subtracted(region2));
    painter.fillPath(painterPath,Qt::red);

    event->accept();
    painter.restore();

1'、QPolygonF绘制中空图形 - 三角板

    QPainter painter(this);
    painter.save();

    //设置反锯齿
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::Qt4CompatiblePainting);

    // 外 大三角形
    QPolygon polygon;
    polygon << QPoint(0,0) << QPoint(0,300)<< QPoint(300,300);
    
    // 内 小三角形
    QPolygon polygon2;
    polygon2 << QPoint(20,20) << QPoint(20,280)<< QPoint(180,280);

    QRegion region(polygon.toPolygon());
    QRegion region2(polygon2.toPolygon());

    QPainterPath painterPath;
    painterPath.addRegion(region.subtracted(region2));
    painter.fillPath(painterPath,Qt::red);

    event->accept();
    painter.restore();

2、使用裁剪,使得只有裁剪区域才能被绘制

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.save();

    //设置反锯齿
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::Qt4CompatiblePainting);
    QRect drawRect = event->rect();
    QRegion region(drawRect.adjusted(10,10,-10,-10),QRegion::Ellipse);
    drawRect.setSize(QSize(drawRect.width()/2,drawRect.height()/2));
    drawRect.moveTopLeft(QPoint((event->rect().width() - drawRect.width())/2,(event->rect().height() - drawRect.height())/2));

    QRegion region2(drawRect,QRegion::Ellipse);
    painter.setClipRegion(region.subtracted(region2));
    painter.fillRect(region.boundingRect(),Qt::yellow);

    event->accept();
    painter.restore();
}

3、在圆的中心画一个与背景颜色相同的圆

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.save();

    //设置反锯齿
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::Qt4CompatiblePainting);

    QRect drawRect = event->rect();
    painter.setBrush(Qt::yellow);
    painter.drawEllipse(drawRect.adjusted(10,10,-10,-10));
    painter.setBrush(painter.background());
    painter.drawEllipse(drawRect.adjusted(drawRect.width()/4,drawRect.height()/4,-drawRect.width()/4,-drawRect.height()/4));

    event->accept();
    painter.restore();
}

4、采用线型渐变,在中心位置到内环半径位置为透明,环部分为颜色值。这里的半径位置在线形渐变中用比例表示,这种只能画圆环,椭圆环不行

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.save();

    //设置反锯齿
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::Qt4CompatiblePainting);
    QRect drawRect = event->rect();

    QRadialGradient rg(drawRect.center(),drawRect.width()/2,drawRect.center());
    rg.setColorAt(0,Qt::transparent);
    rg.setColorAt(0.6,Qt::transparent);
    rg.setColorAt(0.61,Qt::yellow);
    rg.setColorAt(1,Qt::yellow);

    painter.setBrush(rg);
    painter.drawEllipse(drawRect);

    event->accept();
    painter.restore();
}

第一种、第二种有略微锯齿,因为在做区域交叉算法时有取舍,painter设置的反锯齿是给画图操作函数用才有效。

第三种效果较好,可以是圆环图或椭圆环图,需要背景色一致,但这个背景色在painter里面可以直接得到

第四种效果也很好,但只能是圆环,但他可以是其它效果,比如将线形渐变的焦点不设置在圆心,中间不是透明
 

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.save();
    //设置反锯齿
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::Qt4CompatiblePainting);
    QRect drawRect = event->rect();

    QRadialGradient rg(drawRect.center(),drawRect.width()/2,drawRect.center() -= QPoint(30,80));
    rg.setColorAt(0,QColor(Qt::red).darker(400));
    rg.setColorAt(0.6,QColor(Qt::red).darker(150));
    rg.setColorAt(0.61,QColor(Qt::yellow).darker(150));
    rg.setColorAt(1,QColor(Qt::yellow).lighter(200));

    painter.setBrush(rg);
    painter.drawEllipse(drawRect);

    event->accept();
    painter.restore();
}

 

 

 

 

 

你可能感兴趣的:(Qt)