python - 如何获取QGraphicsItem坐标系中的光标点击位置?

标签 python python-3.x pyqt pyqt5 qgraphicsview

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

road_map

附言请不要编写带有鼠标事件处理的代码。如何正确映射点击位置。提前致谢。

最佳答案

想法是将关于场景的坐标转换为关于项目的坐标。

- 在 QGraphicsScene 中覆盖 mousePressEvent:

使用 mapFromScene() 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:

使用 mapFromScene() QGraphicsItem 的方法与 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_())

关于python - 如何获取QGraphicsItem坐标系中的光标点击位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53627056/

相关文章:

python - 重命名 pandas 数据帧的最后一个索引并强制 dtypes

python - MPI 信号处理

python - 基于 Pandas 中子字符串和字符串的混合进行模糊映射的优雅方法

python - 如何从本地计算机或网络资源将图像或图片嵌入到 jupyter notebook 中?

python - QTableWidget 选择多个单元格永久与 Ctrl + MouseClick 相同

python - 从主窗口中的 UI 文件加载具有信号槽定义的小部件

python - 256GB RAM、64 位 python 和 64 位 numpy 的 Numpy 内存错误。限制问题?

python - 如何在 Python 中执行 IMAP 搜索(使用 Gmail 和 imaplib)?

python - 删除/删除重复项,除非行包含特定字符串

qt - 如何控制QWidgetAction的子部件的焦点?