PyQt5:GraphicsScene 的所有项的坐标为 0.0 [英] PyQt5: All Items of GraphicsScene have coordinates 0.0

查看:104
本文介绍了PyQt5:GraphicsScene 的所有项的坐标为 0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了以下来源并对其进行了一些修改,以获得以下小示例:

I used the following source and modified it a bit, to get the following mini example:

import sys
from PyQt5 import QtCore, QtWidgets

class GraphicsScene(QtWidgets.QGraphicsScene):
    def __init__(self):
        super(GraphicsScene, self).__init__()
        self.setSceneRect(0, 0, 600, 400)


    def mousePressEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:
            x = event.scenePos().x()
            y = event.scenePos().y()
            self.addRect(x, y, 100, 100)
        elif event.buttons() == QtCore.Qt.RightButton:
            for elem in self.items():
                print(elem.x())
        super(GraphicsScene, self).mousePressEvent(event)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    scene = GraphicsScene()
    w = QtWidgets.QGraphicsView(scene)
    w.resize(610, 410)
    w.show()
    sys.exit(app.exec_())

这个想法是,通过点击鼠标左键来创建新的矩形(这已经有效了)并通过点击鼠标右键删除最近的矩形.我知道,如何找到最近的矩形,但为此我需要现有矩形的坐标.如果我们向场景中添加一个新矩形,我们会执行以下操作:

The idea is, to create new rectangles by making left mouse clicks (this works already) and delete the nearest rectangle by making a right mouse click. I know, how I can find the nearest rectangle, but for this I need the coordinates of the existing rectangles. If we add a new rectangle to the scene, we do the following:

self.addRect(x, y, 100, 100)

但是如果我遍历场景中的所有元素,并尝试使用此方法获取元素的 x 坐标:

But if I iterate over all elements in the scene, and try to get the x-coordinate of the elements using this:

    for elem in self.items():
        print(elem.x())
        print(elem.pos().x())
        print(elem.scenePos().x())

那么所有的打印输出都是零.我已经看过docu,但正如我理解它,我正在按照文档的建议做.你知道我做错了什么吗?

then all the print-outputs are zero. I had already a look at the docu, but as I understand it I am doing exactly what the docu recommends. Do you know what I am doing wrong?

当然,我可以将所有坐标保存在一个附加列表中,使用该列表中的值计算最近的矩形,使用以下方法删除每个矩形:

Of course, I could save all the coordinates in an additional list, compute the nearest rectangle with the values in that list, delete each rectangle by using:

    for elem in self.items():
        self.removeItem(elem)

并绘制剩余的矩形.但是,我希望有一个更干净的版本.:)

and plot the remaining rectangles. However, I hope there is a cleaner version for this. :)

推荐答案

作为 文档说明:

请注意,item 的几何图形是在 item 坐标中提供的,并且它的位置被初始化为 (0, 0).例如,如果添加了 QRect(50, 50, 100, 100),其左上角将位于相对于项目坐标系中的原点的 (50, 50) 处.

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0). For example, if a QRect(50, 50, 100, 100) is added, its top-left corner will be at (50, 50) relative to the origin in the item's coordinate system.

所以有两种选择:

  • 在位置 (0, 0) 添加一个指定大小的矩形,然后将其移动到所需位置:
    rectItem = self.addRect(0, 0, 100, 100)
    rectItem.setPos(x, y)

  • 使用 addRect 中的坐标并根据矩形的左上角获取实际位置:
  •     for elem in self.items():
            pos = elem.pos()
            if isinstance(elem, QtWidgets.QGraphicsRectItem):
                pos += elem.rect().topLeft()
    

    这篇关于PyQt5:GraphicsScene 的所有项的坐标为 0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆