QPainterPath
QPainterPath类为绘画操作提供了一个容器,允许构造和重用图形形状。
代码一加运算:
void MainWindow::painterPathDemo(QPainter *painter)
{
painter->save();
QPainterPath path;
path.moveTo(200,200);
path.lineTo(220,240);
path.cubicTo(200,0,50,90,100,120);
QPainterPath pathE;
pathE.addEllipse(-100,-100,200,200);
QPainterPath pathR;
pathR.addRect(0,0,100,100);
painter->drawPath(/*path+*/pathR+pathE);
painter->restore();
}
演示图片:
代码二加运算:
void MainWindow::painterPathDemo(QPainter *painter)
{
painter->save();
QPainterPath path;
path.moveTo(200,200);
path.lineTo(220,240);
path.cubicTo(200,0,50,90,100,120);
QPainterPath pathE;
pathE.addEllipse(-100,-100,200,200);
QPainterPath pathR;
pathR.addRect(0,0,100,100);
painter->drawPath(path+pathR+pathE);
painter->restore();
}
演示图片:
代码三减运算Ⅰ:
void MainWindow::painterPathDemo(QPainter *painter)
{
painter->save();
QPainterPath path;
path.moveTo(200,200);
path.lineTo(220,240);
path.cubicTo(200,0,50,90,100,120);
QPainterPath pathE;
pathE.addEllipse(-100,-100,200,200);
QPainterPath pathR;
pathR.addRect(0,0,100,100);
painter->drawPath(/*path+*/pathR-pathE);
painter->restore();
}
代码三减运算Ⅱ:
void MainWindow::painterPathDemo(QPainter *painter)
{
painter->save();
QPainterPath path;
path.moveTo(200,200);
path.lineTo(220,240);
path.cubicTo(200,0,50,90,100,120);
QPainterPath pathE;
pathE.addEllipse(-100,-100,200,200);
QPainterPath pathR;
pathR.addRect(0,0,100,100);
painter->drawPath(/*path+*/pathE-pathR);
painter->restore();
}
代码四或运算:
void MainWindow::painterPathDemo(QPainter *painter)
{
painter->save();
QPainterPath path;
path.moveTo(200,200);
path.lineTo(220,240);
path.cubicTo(200,0,50,90,100,120);
QPainterPath pathE;
pathE.addEllipse(-100,-100,200,200);
QPainterPath pathR;
pathR.addRect(0,0,100,100);
painter->drawPath(/*path+*/pathR|pathE);
painter->restore();
}
代码五与运算:
void MainWindow::painterPathDemo(QPainter *painter)
{
painter->save();
QPainterPath path;
path.moveTo(200,200);
path.lineTo(220,240);
path.cubicTo(200,0,50,90,100,120);
QPainterPath pathE;
pathE.addEllipse(-100,-100,200,200);
QPainterPath pathR;
pathR.addRect(0,0,100,100);
painter->drawPath(/*path+*/pathR&pathE);
painter->restore();
}
总结:
1.QPainterPath可以进行加减与或等运算。
QPainterPath path1,path2;
path1+path2 == void addPath(const QPainterPath &path);
path1-path2 == QPainterPath subtracted(const QPainterPath &p) const;
2.QPainterPath进行相减,如path1-path2,将path1与path2重合的部分去掉后,只保留path1剩余的部分。
3.或运算等同于加运算,可以理解为将重合的部分去掉,不重合的部分保留。
4.与运算保留重合部分,可以理解为将路径上共有的容器保留下来。
上述是部分源代码,整个代码连接如下:
https://download.csdn.net/download/weixin_41882459/12040558
该代码包含了线性渐变、圆锥渐变、半径渐变、填充图,擦除图,绘制饼图,绘制三角形,圆,qpainterpath的例子等代码。