2017-09-24 73 views
1

我想通過編寫Go遊戲實現來教自己python。 我想爲石頭製作一個自定義的QGrapicsItem。爲什麼我的Custom QGraphicsItem不顯示在預期的座標上?

目前它只應繪製一個以給定座標(測試)爲中心的圓。 但由於某種原因,該項目出現在我場景上的不同座標上。 不同的我的意思是,我已經畫出了現場的董事會的網格線,並用我用於網格的相同座標來調用我的Stone項目,但它並沒有出現在預期的網格點上。

我對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()) 

欲瞭解更多情況下整個代碼可以在https://github.com/drseilzug/GoGote/blob/master/GoGote/BoardGUI.py

發現我還是相當新的PyQt5與解釋解決方案,幫助我理解,我錯了,將不勝感激。

謝謝 drseilzug

回答

2

我發現我的錯誤。

問題是,我誤解了解釋QRectF Objects參數的方式。我認爲這四個浮標是頂角和右下角的座標,實際上第一組確實是頂角,而後兩個是矩形的尺寸。

的問題是固定的,通過改變boundingRect到

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