【问题标题】:Determine bounding rect of line in Qt确定Qt中线的边界矩形
【发布时间】:2014-10-02 07:54:25
【问题描述】:

我正在使用QPainterPath 在两点之间画一条线,如下所示:

QPainterPath line;
line.moveTo(start_p);
line.lineTo(end_p);

QPen paintpen(Qt::black);
paintpen.setWidth(1);
painter->setRenderHint(QPainter::Antialiasing);
painter->setBrush(Qt::SolidPattern);
painter->setPen(paintpen);
painter->drawPath(line);

我已将边界矩形定义为:

QRectF Line::boundingRect() const
{
 return QRectF(start_p.x(), start_p.y(), end_p.x(), end_p.y());
}

我在以下情况下正确绘制线条:

start_p.x() < end_p.x() 

和

start_p.y() < end_p.y()

应该如何定义边界矩形,以便无论两点(start_p 和 end_p)的坐标之间的关系如何,都可以正确绘制线?

【问题讨论】:

    标签: qt line bounding-box qpainter


    【解决方案1】:

    你可以尝试标准化你的矩形:

    QRectF Line::boundingRect() const
    {
        return QRectF(start_p.x(), start_p.y(), end_p.x(), end_p.y()).normalized();
    }
    

    【讨论】:

    【解决方案2】:

    你可以:-

    • 检查结束点大于起点的条件并适当设置矩形
    • 返回 QPainterPath 的边界矩形
    • 使用QGraphicsLineItem 而不是重新发明轮子。

    如果您只想要一条线,QGraphicsLineItem 可能是最好的方式。

    【讨论】:

    • 第三个最好!
    【解决方案3】:

    您可以使用QPainterPath::boundingRect,它返回QPainterPath 的边界矩形。您可以将画家路径保留为类成员并在boundingRect 函数中访问它:

    QRectF Line::boundingRect() const
    {
         return line.boundingRect();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-12
      • 2012-01-25
      • 2021-02-22
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      相关资源
      最近更新 更多