简体   繁体   English

为什么我的Custom QGraphicsItem没有显示在预期坐标上?

[英]Why does my Custom QGraphicsItem not show up on the intended coordinates?

I'm trying to teach myself python by writing a Go Game implementation. 我试图通过编写Go Game实现来自学python。 I want to make a custom QGrapicsItem for the Stones. 我想为石头制作一个自定义的QGrapicsItem。

At the moment it shall draw only a circle centered on the given coordinates (testing). 目前,它只能绘制以给定坐标为中心的圆(测试)。 But for some reason the item shows up on different Coordinates on my Scene. 但是由于某些原因,该项目显示在我的场景的不同坐标上。 By different i mean, that i have drawn a Grid of lines for the Board onto the scene and call my Stone item with the same coordinates i used for the grid but it doesn't turn up on the intended gridpoint. 我的意思是说,我已经在场景上绘制了一个棋盘格线网格,并使用与该网格相同的坐标来调用我的Stone项目,但是它并没有出现在预期的网格点上。

My code for the StoneItem: 我的StoneItem代码:

from PyQt5.QtWidgets import QGraphicsItem
from PyQt5.QtCore import QRectF

class Stone(QGraphicsItem):
    """
    A Go stone QGraphicsItem
    x, y :: center Coords of Stone
    rad :: radius
    color:: the stone type
        0, self.empty :: empty
        1, self.black :: black
        2, self.white :: white
    """
    empty = 0
    black = 1
    white = 2

    def __init__(self, x, y, rad, color):
        super().__init__()

        self.x = x
        self.y = y
        self.rad = rad
        self.color = color
        self.setPos(x, y)

    def boundingRect(self):
        rect = QRectF(-self.rad, -self.rad,
                      self.rad, self.rad)
        return rect

    def paint(self, painter, *args, **kwargs):
        painter.drawEllipse(self.boundingRect())

For more context the whole code can be found at https://github.com/drseilzug/GoGote/blob/master/GoGote/BoardGUI.py 有关更多上下文, 参见https://github.com/drseilzug/GoGote/blob/master/GoGote/BoardGUI.py

I'm still fairly new to PyQt5 and a solution with an explanation that helps me understand, where i went wrong would be much appreciated. 我对PyQt5还是一个新手,提供了一个可以帮助我理解我哪里出错的解释的解决方案,将不胜感激。

Thank you, drseilzug 谢谢,drseilzug

I found my error. 我发现了我的错误。

The problem was, that i misunderstood how QRectF Objects parameters are interpreted. 问题是,我误解了QRectF对象参数的解释方式。 I thought the 4 floats were the coordinates of topleft corner and bottom right corner, when in reality the first set are indeed the topleft corner but the second two give the dimension of the rectangle. 我以为4个浮点数是左上角和右下角的坐标,而实际上第一组确实是左上角,而第二组确实是矩形的尺寸。

The issue was fixed by changing boundingRect to 该问题已通过将boundingRect更改为

def boundingRect(self):
    rect = QRectF(-self.rad, -self.rad,
                  2*self.rad, 2*self.rad)
    return rect

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

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