如何使用QPen与QpainterPath

How to use QPen with QpainterPath?

本文关键字:QpainterPath QPen 何使用      更新时间:2023-10-16

我有一个代码:

     QPainterPath groupPath;
     QPen pen; // new
     pen.setCosmetic(1); // new
     groupPath.setPen(pen); // error (error: class "QPainterPath" has no member "setPen")
     groupPath.moveTo(60.0, 40.0);
     groupPath.arcTo(40.0, 35.0, 40.0, 10.0, 180.0, 180.0);
     groupPath.moveTo(40.0, 40.0);
     groupPath.lineTo(40.0, 80.0);
     groupPath.arcTo(40.0, 75.0, 40.0, 10.0, 0.0, 180.0);
     groupPath.lineTo(80.0, 80.0);
     groupPath.lineTo(80.0, 40.0);
     groupPath.closeSubpath();

如何使用setPen在我的代码中使用化妆品?

你不能在QPainterPath上使用setPen(),因为它不是画家,它只是一个路径

你应该创建一个QPainter,使用setPen(),然后绘制路径:

QPainter painter(this);
QPen pen;
pen.setCosmetic(true);
painter.setPen(pen);
QPainterPath groupPath
groupPath.moveTo(60.0, 40.0);
groupPath.arcTo(40.0, 35.0, 40.0, 10.0, 180.0, 180.0);
groupPath.moveTo(40.0, 40.0);
groupPath.lineTo(40.0, 80.0);
groupPath.arcTo(40.0, 75.0, 40.0, 10.0, 0.0, 180.0);
groupPath.lineTo(80.0, 80.0);
groupPath.lineTo(80.0, 40.0);
groupPath.closeSubpath();
painter.drawPath(groupPath);

同样,正如@Andreas所说,pen.setCosmetic(true)是不需要的,因为QPen()的默认构造函数创建了一个宽度为0的笔,这已经是Cosmetic

不知道你的实际问题是什么,但一些备注:

  • 实际上QPen::setCosmetic()需要bool参数;1可以,但true是正确的。
  • 通过默认构造器
  • 创建时,新创建的QPen s的宽度为0
  • 宽度为0的QPen s默认为装饰性

因此,pen.setCosmetic(true)不会有任何影响,你的笔应该是装饰性的(意思是,无论如何,有相同的宽度独立于比例因子)。

最后,正如@zakinster所提到的,QPainterPath没有setPen()方法。