Reputation: 51
I'm trying to teach myself python by writing a Go Game implementation. I want to make a custom QGrapicsItem for the Stones.
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.
My code for the 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
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.
Thank you, drseilzug
Upvotes: 1
Views: 145
Reputation: 51
I found my error.
The problem was, that i misunderstood how QRectF Objects parameters are interpreted. 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.
The issue was fixed by changing boundingRect to
def boundingRect(self):
rect = QRectF(-self.rad, -self.rad,
2*self.rad, 2*self.rad)
return rect
Upvotes: 2