本文介绍了如何在 QGraphicsItem 坐标系中获取光标单击位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QGraphicsScene,其中添加了 QGraphicsItem.假设我单击了绘制绿色圆圈的地图图像 (QGraphicsItem).如何根据此 QGraphicsItem 而不是 QGraphicsScene 坐标系获取点击位置.

I have a QGraphicsScene with QGraphicsItem added to it. Suppose I clicked on a map image (QGraphicsItem) where green circle is drawn. How to get click position in terms of this QGraphicsItem and not QGraphicsScene coordinate system.

P.S.请不要编写带有鼠标事件处理功能的代码.只是如何正确映射点击位置.提前致谢.

推荐答案

这个想法是将相对于场景的坐标转换为相对于物品的坐标.

The idea is to convert the coordinate with respect to the scene to a coordinate with respect to the item.

使用 QGraphicsItem 的 mapFromScene() 方法:>

using the mapFromScene() method of QGraphicsItem:

from PyQt5 import QtCore, QtGui, QtWidgets
import random

class Scene(QtWidgets.QGraphicsScene):
    def __init__(self, parent=None):
        super(Scene, self).__init__(parent)
        pixmap = QtGui.QPixmap(100, 100)
        pixmap.fill(QtCore.Qt.red)

        self.pixmap_item = self.addPixmap(pixmap)
        # random position
        self.pixmap_item.setPos(*random.sample(range(-100, 100), 2))


    def mousePressEvent(self, event):
        items = self.items(event.scenePos())
        for item in items:
            if item is self.pixmap_item:
                print(item.mapFromScene(event.scenePos()))
        super(Scene, self).mousePressEvent(event)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    scene = Scene()
    w = QtWidgets.QGraphicsView(scene)
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

- 覆盖 QGraphicsView 中的 mousePressEvent:

使用 QGraphicsItem 的 mapFromScene() 方法和 mapToScene():

from PyQt5 import QtCore, QtGui, QtWidgets
import random

class View(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(View, self).__init__(QtWidgets.QGraphicsScene(), parent)
        pixmap = QtGui.QPixmap(100, 100)
        pixmap.fill(QtCore.Qt.red)

        self.pixmap_item = self.scene().addPixmap(pixmap)
        # random position
        self.pixmap_item.setPos(*random.sample(range(-100, 100), 2))


    def mousePressEvent(self, event):
        items = self.items(event.pos())
        for item in items:
            if item is self.pixmap_item:
                print(item.mapFromScene(self.mapToScene(event.pos())))
        super(View, self).mousePressEvent(event)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = View()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

- 覆盖 QGraphicsItem 的 mousePressEvent:

from PyQt5 import QtCore, QtGui, QtWidgets
import random

class PixmapItem(QtWidgets.QGraphicsPixmapItem):
    def mousePressEvent(self, event):
        print(event.pos())
        super(PixmapItem, self).mousePressEvent(event)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    scene = QtWidgets.QGraphicsScene()
    w = QtWidgets.QGraphicsView(scene)
    pixmap = QtGui.QPixmap(100, 100)
    pixmap.fill(QtCore.Qt.red)
    item = PixmapItem(pixmap)
    scene.addItem(item)
    item.setPos(*random.sample(range(-100, 100), 2))
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

这篇关于如何在 QGraphicsItem 坐标系中获取光标单击位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 17:10