简体   繁体   中英

Movable QGraphicsLineItem bounding box

I'm trying to add a draggable QtGui.QGraphicsLineItem into pyqtgraph.plotItem.

from PyQt4 import QtCore, QtGui import pyqtgraph as pg

app = QtGui.QApplication([])

w = pg.PlotWidget()
w.show()

line = QtGui.QGraphicsLineItem()
line.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
line.setPen(QtGui.QPen(QtGui.QColor(255, 0, 0), 2))
line.setLine(0, 0, 100, 100)

w.plotItem.addItem(line)

app.exec_()

However, there are several problems: - line width changes when the plot is zoomed - area where the dragging starts includes the whole rectangle bounding box (see picture below) drag_area

I tried the following tricks: 1)

line.setFlag(QtGui.QGraphicsItem.ItemIgnoresTransformations)
w.plotItem.addItem(line)

2)

line.setParentItem(w.plotItem.vb)

But the dragging area problem still persists

You could use the LineSegmentRoi from pyqtgraph.

line = pg.LineSegmentROI([0, 100], [0, 0], pen=(255, 0, 0))
w.plotItem.addItem(line)

Ok, here goes the solution. When we use QtGui.QGraphicsLineItem:

1) For "line width changes when the plot is zoomed" problem, use pen.setCosmetic(True) or create pen with pg.mkPen

2) For "area where the dragging starts includes the whole rectangle bounding box (see picture below)" problem, use mouseDragEvent instead of using line.setFlag(QtGui.QGraphicsItem.ItemIsMovable)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM