简体   繁体   English

可移动QGraphicsLineItem边界框

[英]Movable QGraphicsLineItem bounding box

I'm trying to add a draggable QtGui.QGraphicsLineItem into pyqtgraph.plotItem. 我正在尝试将可拖动的QtGui.QGraphicsLineItem添加到pyqtgraph.plotItem中。

from PyQt4 import QtCore, QtGui import pyqtgraph as pg 从PyQt4导入QtCore,QtGui导入pyqtgraph作为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) 我尝试了以下技巧:1)

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

2) 2)

line.setParentItem(w.plotItem.vb)

But the dragging area problem still persists 但是拖动区域问题仍然存在

You could use the LineSegmentRoi from pyqtgraph. 您可以使用pyqtgraph中的LineSegmentRoi

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: 当我们使用QtGui.QGraphicsLineItem时:

1) For "line width changes when the plot is zoomed" problem, use pen.setCosmetic(True) or create pen with pg.mkPen 1)对于“缩放绘图时线宽发生变化”的问题,请使用pen.setCosmetic(True)或使用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) 2)对于“开始拖动的区域包括整个矩形边界框(请参见下图)”的问题,请使用mouseDragEvent而不是使用line.setFlag(QtGui.QGraphicsItem.ItemIsMovable)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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