0

I am currently programming a chess game for a school project. To create the Human-Machine Interface, I am using PyQt5.

In order to draw the chessboard, I've built the following function :

def drawRectangles(self, qp):
    taille = self.case
    for i in range(8):
        for j in range(1, 9):
            s = i + j
            c = s % 2
            qp.setBrush(QtGui.QColor(c * 250, c * 250, c * 250))
            qp.drawRect(100 + i * taille, j * taille, taille, taille)

However, when I click on a chess piece on the chessboard, I wish to paint in a different color (let's say in red) the area where said chess piece can move. In order to do so, I've built the following function :

def reachable_cells(self, cells):
    size = self.size
    qp = QtGui.QPainter()
    qp.begin(self)
    qp.setBrush(QtGui.QColor(0, 0, 150))
    for i in range(len(cells)):
        cell = cells[i]
        qp.drawRect(100 + cell[0] * size, cell[1] * size, size, size)

However, this doesn't have any effect on my chessboard, and the cells which are reachable for a given chesspiece remain black or white.

Would anyone have some tips on how to solve that problem ?

In the event where it isn't possible to do something like that, is it possible to overlay a reachable chess cell with a circle picture ?

Thanks for any help !

  • 1
    1) If you are painting in widget then you must do it in the paintEvent method, 2) Your QPainter does not have any associated device so it does not paint anything, as you will see in the duplicate you have to do `qp = QtGui.QPainter(self)` inside `paintEvent` method. – eyllanesc Apr 30 '21 at 07:33
  • 1
    Note: There are literally thousands of examples that do more or less what you want, for example check my answers: https://stackoverflow.com/search?q=user%3A6622587+paintEvent+%5Bpython%5D+mousePressEvent – eyllanesc Apr 30 '21 at 07:34

0 Answers0