3

I am doing a Qt program where I have rectangles linked with wires (placed by the user with mouse events). Every wire checks if there is a rectangle at the beginning and the end of it. If it is the case, the wire is placed.

I recently wanted to change my rectangles into horizontal lines (better visual), so I wrote:

QRectF myRect(-15, 0, 30, 1);

Instead of a regular rect. The problem is that now it is too hard to trace my wires because the user must link 2 lines with the mouse, which is almost impossible.

I tried to change bounding rect/shape but none of them works:

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 bounding rect & shape are only used for selecting. I also tried to use an image (desperate solution), but I can't find a way to add an image/pixmap to my QpainterPath.

I know I can use a line instead of a flat rectangle, but the problem is still the same.

Thanks for helping me :)

Tarask
  • 31
  • 1

1 Answers1

0
  1. Use the QPainterPath for the check rectangle as you have mentioned
  2. Get the End points of the Wire in QPointF( Two Points)

  3. Use bool QPainterPath::contains(const QPointF & point)to check if the wire is within the QPainterPath (Two checks for the start and end point of the wire)

or this in case of a wire image that is dragged and dropped,

  1. Use the QPainterPath for the check rectangle
  2. If wire is an Image get the QRect of the Wire using QPixMap::rect()
  3. Use bool QPainterPath::contains(const QRectF & rectangle) to check if the wire is within the QPainterPath
techneaz
  • 998
  • 6
  • 9