gpt4 book ai didi

python - 使用橡皮筋选择 QGraphicsItem

转载 作者:行者123 更新时间:2023-12-01 01:02:10 26 4
gpt4 key购买 nike

我正在尝试使用rubberBand来选择一些与某些预定的X和Y坐标关联的QGraphicItems。用户可以使用鼠标滚轮进行缩放,并使用鼠标中心按钮进行平移。用户可以单击鼠标左键并绘制一个选择窗口,但我当前无法选择任何点。我找到了一个 example,我正在尝试适应我的,但现在它找不到我的自定义 Point 对象。

我假设我的 self.findChildren(QGraphicsItem) 出了问题,并且因为我使用自己的自定义 QGraphicsItem 它无法找到那些已添加的内容。我尝试将 findChildren 函数与 Point 对象一起使用,但得到了相同的结果。非常感谢任何建议和帮助。

当前代码

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
from math import sqrt

class LogObject(QObject):
hovered = pyqtSignal()
notHovered = pyqtSignal()

class Point(QGraphicsItem):

def __init__(self, x, y,name):
super(Point, self).__init__()
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.rectF = QRectF(0, 0, 30, 30)
self.x=x
self.y=y
self.name = name
self._brush = QBrush(Qt.black)
self.setAcceptHoverEvents(True)
self.log = LogObject()

def setBrush(self, brush):
self._brush = brush
self.update()

def boundingRect(self):
return self.rectF

def paint(self, painter=None, style=None, widget=None):
painter.fillRect(self.rectF, self._brush)

def itemChange(self, change, value):
if change == self.ItemSelectedChange:
if value:
self._brush = QBrush(Qt.green)
else:
self._brush = QBrush(Qt.black)
return QGraphicsItem.itemChange(self, change, value)

def hoverEnterEvent(self, event):
self.setBrush(QColor("red"))
self.log.hovered.emit()
QGraphicsItem.hoverMoveEvent(self, event)

def hoverLeaveEvent(self, event):
self.setBrush(QColor("black"))
self.log.notHovered.emit()
QGraphicsItem.hoverMoveEvent(self, event)

def mousePressEvent(self, event):
print(self.name)
QGraphicsItem.mousePressEvent(self, event)


class Viewer(QGraphicsView):
photoClicked = pyqtSignal(QPoint)
rectChanged = pyqtSignal(QRect)

def __init__(self, parent):
super(Viewer, self).__init__(parent)
self.rubberBand = QRubberBand(QRubberBand.Rectangle, self)
self.setMouseTracking(True)
self.origin = QPoint()
self.changeRubberBand = False

self._zoom = 0
self._empty = True
self._scene = QGraphicsScene(self)

self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setFrameShape(QFrame.NoFrame)
self.area = float()
self.setPoints()
QTimer.singleShot(0, self.fitInView) # This is done so that it can fit into view on load
self.viewport().setCursor(Qt.ArrowCursor)

def setItems(self):
self.data = {'x': [-2414943.8686, -2417160.6592, -2417160.6592, -2417856.1783, -2417054.7618, -2416009.9966, -2416012.5232, -2418160.8952, -2418160.8952, -2416012.5232, -2417094.7694, -2417094.7694], 'y': [10454269.7008,
10454147.2672, 10454147.2672, 10453285.2456, 10452556.8132, 10453240.2808, 10455255.8752, 10455183.1912, 10455183.1912, 10455255.8752, 10456212.5959, 10456212.5959]}
maxX = max(self.data['x'])
minX = min(self.data['x'])
maxY = max(self.data['y'])
minY = min(self.data['y'])
distance = sqrt((maxX-minX)**2+(maxY-minY)**2)

self.area = QRectF(minX, minY, distance, distance)
for i,x in enumerate(self.data['x']):
x = self.data['x'][i]
y = self.data['y'][i]
p = Point(x,y,'Point__'+str(i))
p.log.hovered.connect(self.hoverChange)
p.log.notHovered.connect(self.notHoverChange)
p.setPos(x,y)
self._scene.addItem(p)
self.setScene(self._scene)



def fitInView(self, scale=True):
rect = QRectF(self.area)
if not rect.isNull():
self.setSceneRect(rect)

unity = self.transform().mapRect(QRectF(0, 0, 1, 1))
self.scale(1 / unity.width(), 1 / unity.height())
viewrect = self.viewport().rect()
scenerect = self.transform().mapRect(rect)
factor = min(viewrect.width() / scenerect.width(),
viewrect.height() / scenerect.height())
self.scale(factor, factor)
self._zoom = 0


def setPoints(self):
self._zoom = 0
self.setItems()
self.setDragMode(self.ScrollHandDrag)

def wheelEvent(self, event):
if event.angleDelta().y() > 0:
factor = 1.25
self._zoom += 1
else:
factor = 0.8
self._zoom -= 1
if self._zoom > 0:
self.scale(factor, factor)
elif self._zoom == 0:
self.fitInView()
else:
self._zoom = 0

def hoverChange(self):
self.viewport().setCursor(Qt.PointingHandCursor)

def notHoverChange(self):
self.viewport().setCursor(Qt.ArrowCursor)

def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.origin = event.pos()
self.rubberBand.setGeometry(QRect(self.origin, QSize()))
self.rectChanged.emit(self.rubberBand.geometry())
self.rubberBand.show()
self.changeRubberBand = True
return
#QGraphicsView.mousePressEvent(self,event)
elif event.button() == Qt.MidButton:
self.viewport().setCursor(Qt.ClosedHandCursor)
self.original_event = event
handmade_event = QMouseEvent(QEvent.MouseButtonPress,QPointF(event.pos()),Qt.LeftButton,event.buttons(),Qt.KeyboardModifiers())
QGraphicsView.mousePressEvent(self,handmade_event)

super(Viewer, self).mousePressEvent(event)

def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.changeRubberBand = False
if self.rubberBand.isVisible():
self.rubberBand.hide()
selected = []
rect = self.rubberBand.geometry()
for child in self.findChildren(QGraphicsItem):
if rect.intersects(child.geometry()):
selected.append(child)
print ('Selection Contains:\n '),
if selected:
print (' '.join(
'Item: %s\n' % child.name for child in selected))
else:
print (' Nothing\n')
QGraphicsView.mouseReleaseEvent(self,event)
elif event.button() == Qt.MidButton:
self.viewport().setCursor(Qt.ArrowCursor)
handmade_event = QMouseEvent(QEvent.MouseButtonRelease,QPointF(event.pos()),Qt.LeftButton,event.buttons(),Qt.KeyboardModifiers())
QGraphicsView.mouseReleaseEvent(self,handmade_event)
super(Viewer, self).mouseReleaseEvent(event)


def mouseMoveEvent(self, event):
if self.changeRubberBand:
self.rubberBand.setGeometry(QRect(self.origin, event.pos()).normalized())
self.rectChanged.emit(self.rubberBand.geometry())
QGraphicsView.mouseMoveEvent(self,event)
super(Viewer, self).mouseMoveEvent(event)

class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.viewer = Viewer(self)
self.btnLoad = QToolButton(self)
self.btnLoad.setText('Fit Into View')
self.btnLoad.clicked.connect(self.fitPoints)

VBlayout = QVBoxLayout(self)
VBlayout.addWidget(self.viewer)
HBlayout = QHBoxLayout()
HBlayout.setAlignment(Qt.AlignLeft)
HBlayout.addWidget(self.btnLoad)

VBlayout.addLayout(HBlayout)
self.viewer.fitInView()

def fitPoints(self):
self.viewer.fitInView()



if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 800, 600)
window.show()
sys.exit(app.exec_())

最佳答案

findChildren 方法查找 Viewer 的 QObject 子级,但 QGraphicsItems 不是 QObject,也不是 Viewer 的子级,因此它不是正确的解决方案。

该过程是使用mapToScene方法将Viewer视口(viewport)坐标中的QRubberBand矩形转换为场景,然后使用方法items()传递场景坐标中的矩形。

def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.changeRubberBand = False
if self.rubberBand.isVisible():
self.rubberBand.hide()
rect = self.rubberBand.geometry()
rect_scene = self.mapToScene(rect).boundingRect()
selected = self.scene().items(rect_scene)
if selected:
print(
"".join("Item: %s\n" % child.name for child in selected)
)
else:
print(" Nothing\n")
QGraphicsView.mouseReleaseEvent(self, event)
# ...

另一方面,我看到您正在基于 QGraphicsItem 创建 Point,最简单的是它继承自 QGraphicsRectItem:

class Point(QGraphicsRectItem):
def __init__(self, x, y, name):
super(Point, self).__init__(QRectF(0, 0, 30, 30))
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.name = name
self.setBrush(QBrush(Qt.black))
self.setAcceptHoverEvents(True)
self.log = LogObject()
self.setPos(x, y)

def itemChange(self, change, value):
if change == self.ItemSelectedChange:
self.setBrush(QBrush(Qt.green) if value else QBrush(Qt.black))
return QGraphicsItem.itemChange(self, change, value)

def hoverEnterEvent(self, event):
self.setBrush(QColor("red"))
self.log.hovered.emit()
QGraphicsItem.hoverMoveEvent(self, event)

def hoverLeaveEvent(self, event):
self.setBrush(QColor("black"))
self.log.notHovered.emit()
QGraphicsItem.hoverMoveEvent(self, event)

def mousePressEvent(self, event):
print(self.name)
QGraphicsItem.mousePressEvent(self, event)

# ...
class Viewer(QGraphicsView):
# ...
def setItems(self):
# ...
for i, (x, y) in enumerate(zip(self.data["x"], self.data["y"])):
p = Point(x, y, "Point__" + str(i))
p.log.hovered.connect(self.hoverChange)
p.log.notHovered.connect(self.notHoverChange)
self._scene.addItem(p)
self.setScene(self._scene)
# ...

关于python - 使用橡皮筋选择 QGraphicsItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55697809/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com