美文网首页
QPainter创建路径

QPainter创建路径

作者: 雯饰太一 | 来源:发表于2023-06-20 15:34 被阅读0次

    路径的交叉并补可以通过函数或者操作符进行操作,如下:

    //函数
    QPainterPath subtracted(const QPainterPath &p) const;   //差集
    QPainterPath united(const QPainterPath &p) const;       //并集
    QPainterPath intersected(const QPainterPath &p) const;//交集
    //操作符
    QPainterPath operator&(const QPainterPath &other) const;
    QPainterPath &operator&=(const QPainterPath &other);
    QPainterPath operator+(const QPainterPath &other) const;
    QPainterPath &operator+=(const QPainterPath &other);
    QPainterPath operator-(const QPainterPath &other) const;
    QPainterPath &operator-=(const QPainterPath &other);
    

    示例代码:

    void QwIFNTProgressBar::Pate_Test3(QPainter& painter)
    {
        painter.save();
        painter.setWindow(0, 0, 400, 400);
        painter.translate(200,0);
        painter.scale(0.8, 0.8);
    
        painter.setRenderHint(QPainter::Antialiasing);
        QPainterPath path;
        path.lineTo(100, 0);
        path.cubicTo(200, 0, 100, 50, 200, 100);
        path.closeSubpath();
        path.addRect(50, 50, 50, 50);
    
        QPainterPath path2;
        path2.addEllipse(80, 30, 50, 50);
        path = path.subtracted(path2);
    
        QPoint offsetPoint(20, 20);
        painter.translate(offsetPoint);
        painter.setBrush(Qt::lightGray);
        painter.drawPath(path);
        painter.setBrush(Qt::NoBrush);
        painter.drawRect(path.boundingRect());
    
        QPen pen;
        pen.setWidth(5);
        pen.setColor(Qt::red);
        painter.setPen(pen);
        painter.drawPoint(QPoint(100, 0) );
        painter.drawPoint(QPoint(200, 0) );
        painter.drawPoint(QPoint(100, 50) );
        painter.drawPoint(QPoint(200, 100) );
    
        painter.restore();
    }
    

    相关文章

      网友评论

          本文标题:QPainter创建路径

          本文链接:https://www.haomeiwen.com/subject/kpmredtx.html