QGraphicsScene Is not performing proper hit detection - qt

I am having an issue where my QGraphicsItem is not alway performing proper hit detection with the mouse. I have subclassed QGrahicsItem and have overridden the shape() method. My shape() method calculates a polygon that surrounds the line. my boudingRect() function calculates a box that completely encloses the line. I have attached two screenshots. The first show the line highlighted in blue, indicating the hover event was fired. The second show the mouse moved just slightly to the right, but still well within the shape() and boundingRect() but the hover event did not fire. Any ideas on what I'm doing wrong?
I should also note that if I move the endpoints of the line, the hit detection usually starts working normally. It seems somewhat random as to when it breaks.
Note: The polygon surrounding the line is the shape() and the rectangle around that is the boundingRect()
Update
It seems that this must be a problem with the boundingRect(), as i zoom in to a non-working/partially-working line. The line will suddently disappear as if it's boundingRect() is no longer in the viewport, however, this is very obviously not the case as just before it disappears the bounding rect is visible.

Related

How to remove Ghost Lines drawn in qgraphicsview

I am trying to make a simple program in which I have added a qgraphics scene and in this I have added a QGraphicsRectItem. I have implemented mouse press event, paint event, bounding rect. Now I have drawn a point on one side of rectangle because there can be multiple rectangle I can drop on screen so just to differentiate between them of different color. Now I can move my rectangle inside graphics seen and can increase the size of rectangle by moving it's one side at a time. The problem that I am facing is when I trying to draw point on one side of rectangle at the time of moving it, it leaves traces on graphics scene. can I remove the ghost lines?
This happens either because your boundingRect method isn't correct, or because you forgot to call prepareGeometryChange before making changes that affect the boundingRect result. Your boundingRect needs to include space for line widths, for example; that's a common mistake.

How to pass hoverMoveEvent to underlaying items

I have custom graphic item that draws some figure consisting of lines and polygons.
It has reimplemented method
hoverMoveEvent( QGraphicsSceneHoverEvent* event )
that indicates when need to highlight the figure (when mouse crosses the internal line or polygon ).
There are situations when items are drawn one above other, but hover event is accepted only by top item.
I tried to ignore event inside the method but it don't help.
To solve this problem need to reimplement method
QPainterPath QGraphicsItem::shape() const
in the custom item.
This shape should be exactly as a shape of "internal" painted items. Without empty spaces inside the bounding rectangle.

Shadow Effects with QGraphicsRectItem in Qt

I have a QGraphicsRectItem over a scene. I intend to drag and drop this window over the scene. When the rect item reaches the left boundary end I have to show it appearing from the right end. Currently I am using two objects and hiding and showing them by calculating the boundary of scene which involves lot of calculations.
Is there any better way to achieve the same effect using just a single object?
Thank You
You could use a single item that spans the entire scene, and draw the rectangle (or 2 parts of it) in it's paint method.
But then you would lose the optimization of the BSP tree, your item would redraw even if some unrelated area repaints. If this is just 1 item, I guess it would not have much impact.
You would need to implement your own dragging with mousemove event and the like, though this is not much code, you just need to get it right.

QGraphicsView not drawing line items when zoomed in or moved?

I'm developing a small Qt application similar to the DiagramScene example. I have subclassed QGraphicsView instead of QGraphicsScene. My view is zooming in and out with the mouseWheel, I can drag it with the mouse and I can add Nodes and Links with clicking.
I click on one Node, (the first end of the line item is set), then move the mouse (the second end of the line is following the mouse cursor), then I click on the second Node and anchor the second end of the line item to this second node.
The problem is, when the view is zoomed in, or I have moved the view, when I click on a Node and move the mouse, the preview of the link is not visible. When I click on the second Node - the link is still not visible. The link between the two Nodes becomes visible only after I zoom out or drag the view to some point and it intersects with the sides of the view.
Any ideas how to fix this?
Thank you so much in advance.
I finally fixed it. I was wrong to use data members for the coordinates (also the bounding rect and paint method) of my custom Graphics items. I changed the code using the setPos() function which gives the right coordinates to my items. #Merlin069 thank you, actually your last question got me thinking whether I set the coordinates correctly.

QRubberBand Selection - Stopping dotted line around selected items

I am selecting items using the default QRubberBand enabled by setting QGraphicsView.setDragMode() to rubber band selection. This works fine, however when an item is selected it appears with a grey dotted line around it's bounding rect and I was wondering if there is any way to prevent this?
It appears the only way to do this is to override QGraphicsItem::paint().
The default implementation of paint() for standard items will change the pen if the item is selected or not. But since standard items likely consist of 1 line paint() calls changing this is trivial.
For example in a QGraphicsLineItem it would just be painter.drawLine( line() );

Resources