Qt bounding box / shape for element interaction
I am executing a Qt program where I have rectangles connected with wires (placed by the user with mouse events). Each wire checks if there is a rectangle at the beginning and at the end. If so, the wire is placed.
I recently wanted to change my rectangles to horizontal lines (better visually), so I wrote:
QRectF myRect(-15, 0, 30, 1);
Instead of a regular rectangle. The problem is that it is now too difficult to track my wires because the user has to link two strings with the mouse, which is almost impossible.
I tried to change the bounding box / shape, but none of them work:
QRectF Port::boundingRect()
{
return QRectF(-15, 0, 30, 10);
}
QPainterPath Port::shape()
{
QPainterPath path;
path.addRect(-15, 0, 30, 10);
return path;
}
I think the problem is that the bounding rectangle and shape are only used for selection. I've also tried using an image (desperate solution), but I can't seem to find a way to add the image / pixmap to my QpainterPath.
I know I can use a string instead of a flat rectangle, but the problem remains the same.
Thanks for helping me :)
source to share
- Use
QPainterPath
for the validation rectangle as you mentioned -
Get the end points of the wire at
QPointF
(two points) -
Use
bool QPainterPath::contains(const QPointF & point)
to check if the wire is withinQPainterPath
(Two checks for the start and end point of the wire)
or is this in the case of a wired image that is dragged and dropped,
- Use
QPainterPath
for check rectangle - If the wire is an image, we get the
QRect
wires usingQPixMap::rect()
- Use
bool QPainterPath::contains(const QRectF & rectangle)
to check if the wire is withinQPainterPath
source to share