From a465f93d9bf1e63b0ed8a233f9ac3f4be0e13eaa Mon Sep 17 00:00:00 2001 From: Martin Chase Date: Wed, 14 Apr 2021 10:44:03 -0700 Subject: [PATCH] small TargetItem fixes (#1715) * angle is just an int, not a method * more TargetItem use in the example * offset is needed before super sometimes * use setLabel, too --- examples/InfiniteLine.py | 21 +++++++++------------ pyqtgraph/graphicsItems/TargetItem.py | 13 +++++++------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/examples/InfiniteLine.py b/examples/InfiniteLine.py index 07216be2..78ead34b 100644 --- a/examples/InfiniteLine.py +++ b/examples/InfiniteLine.py @@ -32,33 +32,29 @@ p1.addItem(inf1) p1.addItem(inf2) p1.addItem(inf3) -targetItem1 = pg.TargetItem( - label=True, - symbol="crosshair", - labelOpts={ - "angle": 0 - } -) +targetItem1 = pg.TargetItem() targetItem2 = pg.TargetItem( pos=(30, 5), size=20, - label="vert={1:0.2f}", symbol="star", pen="#F4511E", + label="vert={1:0.2f}", labelOpts={ - "angle": 45, "offset": QtCore.QPoint(15, 15) } ) +targetItem2.label().setAngle(45) targetItem3 = pg.TargetItem( pos=(10, 10), size=10, - label="Third Label", - symbol="x", + symbol="x", pen="#00ACC1", - labelOpts={ +) +targetItem3.setLabel( + "Third Label", + { "anchor": QtCore.QPointF(0.5, 0.5), "offset": QtCore.QPointF(30, 0), "color": "#558B2F", @@ -66,6 +62,7 @@ targetItem3 = pg.TargetItem( } ) + def callableFunction(x, y): return f"Square Values: ({x**2:.4f}, {y**2:.4f})" diff --git a/pyqtgraph/graphicsItems/TargetItem.py b/pyqtgraph/graphicsItems/TargetItem.py index b599f33b..26f40b9b 100644 --- a/pyqtgraph/graphicsItems/TargetItem.py +++ b/pyqtgraph/graphicsItems/TargetItem.py @@ -354,7 +354,7 @@ class TargetItem(UIGraphicsItem): DeprecationWarning, stacklevel=2, ) - if self.label() is not None and angle != self.label().angle(): + if self.label() is not None and angle != self.label().angle: self.label().setAngle(angle) return None @@ -393,11 +393,6 @@ class TargetLabel(TextItem): anchor=(0, 0.5), **kwargs, ): - super().__init__(anchor=anchor, **kwargs) - self.setParentItem(target) - self.target = target - self.setFormat(text) - if isinstance(offset, Point): self.offset = offset elif isinstance(offset, (tuple, list)): @@ -406,6 +401,12 @@ class TargetLabel(TextItem): self.offset = Point(offset.x(), offset.y()) else: raise TypeError("Offset parameter is the wrong data type") + + super().__init__(anchor=anchor, **kwargs) + self.setParentItem(target) + self.target = target + self.setFormat(text) + self.target.sigPositionChanged.connect(self.valueChanged) self.valueChanged()