We've encountered something similar with our version of InfiniteLine which
we haven't ironed out.  A first thing you can try is to redefine your
"shape" method to something like....

def shape(self)
    p = self.path
    p.addRect(self.boundingRect())
    return p

For debugging purposes, you may want to draw the boundingRect around the
line to see if the rectangle has some 0-thickness making it impossible to
register mouse events.

On Sun, Jul 10, 2022 at 11:00 AM 你你 <[email protected]> wrote:

> Hi, I implemented a  LineItem class finally inheriting QGraphicsPathItem,
> to draw a simple line by specifying a start point and an end point. It
> works fine only there's a problem regarding mouse hover. It's really
> strange that when I draw a horizontal line, no matter how I tried the mouse
> hover or mouse click, there will be no  hoverEnterEvent nor
> mouseClickEvent triggered. However, if I draw a line that's not horizontal,
> both the  hoverEnterEvent and mouseClickEvent will be triggered. My code's
> shown below, does anyone know what's going on here, what else do I need to
> add to my code ? Many thanks! [image: lines.png]
>
> class LineItem(GraphicsPathItem,QtCore.QObject):
>     sigHoveredEnter = QtCore.Signal(object, object,object)
>     sigHoveredLeave = QtCore.Signal(object, object)
>     sigClicked = QtCore.Signal(object, object)
>     def __init__(self,start=None, end=None, data = None, parent=None,
> **opts) -> None:
>         self._start = start
>         self._end = end
>         self.data = data
>         QtCore.QObject.__init__(self)
>         GraphicsPathItem.__init__(self, parent, **opts)
>         self.setAcceptHoverEvents(True)
>
>     def hoverEnterEvent(self, ev):
>         self.sigHoveredEnter.emit(self, ev, self.data)
>         logger.debug('line Enter')
>
>     def makePath(self):
>         path = QtGui.QPainterPath()
>         path.moveTo(*self._start)
>         path.lineTo(*self._end)
>         return path
>
>     def mouseClickEvent(self, ev):
>         self.sigClicked.emit(self, ev, self.data)
>         logger.debug('line clicked.')
>
>
> class GraphicsPathItem(QtGui.QGraphicsPathItem):
>     __metaclass__ = ABCMeta
>     def __init__(self, parent=None, **opts) -> None:
>         self.opts = {}
>         QtGui.QGraphicsPathItem.__init__(self, parent)
>         defaultOpts = {
>             'pxMode': False,
>             'pen': (200,200,200),
>             'brush': (190, 190, 190),
>         }
>         defaultOpts.update(opts)
>
>         self.setStyle(**defaultOpts)
>
>     def setStyle(self, **opts):
>         needUpdate = False
>         for k,v in opts.items():
>             if self.opts.get(k) != v:
>                 needUpdate = True
>             self.opts[k] = v
>
>         if not needUpdate:
>             return
>
>         tr = QtGui.QTransform()
>         self.path = tr.map(self.makePath())
>
>         self.setPath(self.path)
>
>         self.setPen(fn.mkPen(self.opts['pen']))
>         self.setBrush(fn.mkBrush(self.opts['brush']))
>
>         if self.opts['pxMode']:
>             self.setFlags(self.flags() |
> self.GraphicsItemFlag.ItemIgnoresTransformations)
>         else:
>             self.setFlags(self.flags() &
> ~self.GraphicsItemFlag.ItemIgnoresTransformations)
>
>     @abstractmethod
>     def makePath(self):
>         pass
>
>     def paint(self, p, *args):
>         p.setRenderHint(QtGui.QPainter.RenderHint.Antialiasing)
>         super().paint(p, *args)
>
>     def shape(self):
>         return self.path
>
>     def dataBounds(self, ax, frac, orthoRange=None):
>         pw = 0
>         pen = self.pen()
>         if not pen.isCosmetic():
>             pw = pen.width() * 0.7072
>         if self.opts['pxMode']:
>             return [0,0]
>         else:
>             br = self.boundingRect()
>             if ax == 0:
>                 return [br.left()-pw, br.right()+pw]
>             else:
>                 return [br.top()-pw, br.bottom()+pw]
>
>     def pixelPadding(self):
>         pad = 0
>         if self.opts['pxMode']:
>             br = self.boundingRect()
>             pad += hypot(br.width(), br.height())
>         pen = self.pen()
>         if pen.isCosmetic():
>             pad += max(1, pen.width()) * 0.7072
>         return pad
>
> --
> You received this message because you are subscribed to the Google Groups
> "pyqtgraph" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pyqtgraph/a9da8789-a777-40be-b98f-1510c9c65400n%40googlegroups.com
> <https://groups.google.com/d/msgid/pyqtgraph/a9da8789-a777-40be-b98f-1510c9c65400n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pyqtgraph/CA%2BnduTF_5-Lt1qjCXhMBuS-_58jna_oHQYdFjaABT2XozPueWg%40mail.gmail.com.

Reply via email to