I am drawing a line using QPainterPath
between two points as follows:
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);
I have defined bounding rect as:
QRectF Line::boundingRect() const
{
return QRectF(start_p.x(), start_p.y(), end_p.x(), end_p.y());
}
I get line painted correctly when:
start_p.x() < end_p.x()
and
start_p.y() < end_p.y()
How should the bounding rect be defined so that line is drawn correctly irrespective of the relationship between the coordinates of two points(start_p and end_p)?
You might try to normalize your rectangle:
QRectF Line::boundingRect() const
{
return QRectF(start_p.x(), start_p.y(), end_p.x(), end_p.y()).normalized();
}
You could either:-
If you just want a line, QGraphicsLineItem is likely the best way to go here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With