PySide - PyQt4 - Draw on one table widget
Question: In the following code, I would like to draw fro exemple a circle on the cell
of coordinate (4,5). How could I achieve this ?

    
    
    #! /usr/bin/env python2.7
    # -*- coding: utf-8 -*-
    
    import sys
    
    from PySide import QtCore, QtGui
    
    class MainWindow(QtGui.QWidget):
        def __init__(
            self,
            parent = None
        ):
            super(MainWindow, self).__init__(parent)
    
    # General grid
            self.table = QtGui.QTableWidget(self)
            self.nbrow, self.nbcol = 9, 9
            self.table.setRowCount(self.nbrow)
            self.table.setColumnCount(self.nbcol)
    
    # Each cell has dimension 50 pixels x 50 pixels
            for row in range(0, self.nbrow):
                self.table.setRowHeight(row, 50)
    
                for col in range(0, self.nbcol):
                    self.table.setColumnWidth(col, 50)
    
    # Each cell contains one single QTableWidgetItem
            for row in range(0, self.nbrow):
                for col in range(0, self.nbcol):
                    item = QtGui.QTableWidgetItem()
                    item.setTextAlignment(
                        QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter
                    )
    
                    self.table.setItem(row, col, item)
    
    # Header formatting
            font = QtGui.QFont()
            font.setFamily(u"DejaVu Sans")
            font.setPointSize(12)
            self.table.horizontalHeader().setFont(font)
            self.table.verticalHeader().setFont(font)
    
    # Font used
            font = QtGui.QFont()
            font.setFamily(u"DejaVu Sans")
            font.setPointSize(20)
            self.table.setFont(font)
    
    # Global Size
            self.resize(60*9, 60*9 + 20)
    
    # Layout of the table
            layout = QtGui.QGridLayout()
            layout.addWidget(self.table, 0, 0)
            self.setLayout(layout)
    
    # Set the focus in the first cell
            self.table.setFocus()
            self.table.setCurrentCell(0, 0)
    
    
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        fen = MainWindow()
        fen.show()
        sys.exit(app.exec_())
    


Answer: I guess you can reimplement the paint method like in the following example
which captures a right-mouse click and places a little circle on the
corresponding coordinates.

    
    
    #!/usr/bin/python
    #-*- coding:utf-8 -*-
    
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
    
    class PaintTable(QTableWidget):
        def __init__(self, parent):
            QTableWidget.__init__(self, parent)
            self.setRowCount(10) 
            self.setColumnCount(10)
            self.center = QPoint(-10,-10)
    
        def paintEvent(self, event):
            painter = QPainter(self.viewport()) #See: http://stackoverflow.com/questions/12226930/overriding-qpaintevents-in-pyqt
            painter.drawEllipse(self.center,10,10)
            QTableWidget.paintEvent(self,event)
    
        def mousePressEvent(self, event):
            if event.buttons() == Qt.RightButton:
                self.center = QPoint(event.pos().x(),  event.pos().y())
                print self.center
                self.viewport().repaint()
    
            elif event.buttons() == Qt.LeftButton:
                QTableWidget.mousePressEvent(self,event)
    
    
    class mainWindow(QMainWindow):
        def __init__(self, parent=None):
            super(mainWindow, self).__init__(parent)
    
            self.table = PaintTable(self)
            self.setCentralWidget(self.table)
    
    
    if __name__ == "__main__":
        import  sys
    
        app = QApplication(sys.argv)
        main = mainWindow()
        main.show()
        sys.exit(app.exec_())
    

Applied to your minimal working example this would look like this:

    
    
    #! /usr/bin/env python2.7
    # -*- coding: utf-8 -*-
    
    import sys
    
    #from PyQt4 import QtCore, QtGui 
    from PySide import QtCore, QtGui #Just tested it for PyQt4 since I don't have PySide installed...
    
    
    class PaintTable(QtGui.QTableWidget):
        def __init__(self, parent):
            QtGui.QTableWidget.__init__(self, parent)
            self.center = QtCore.QPoint(-10,-10)
    
        def paintEvent(self, event):
            painter = QtGui.QPainter(self.viewport()) #See: http://stackoverflow.com/questions/12226930/overriding-qpaintevents-in-pyqt
            painter.drawEllipse(self.center,10,10)
            QtGui.QTableWidget.paintEvent(self,event)
    
        def mousePressEvent(self, event):
            if event.buttons() == QtCore.Qt.RightButton:
                self.center = QtCore.QPoint(event.pos().x(),  event.pos().y())
                print self.center
                self.viewport().repaint()
    
            elif event.buttons() == QtCore.Qt.LeftButton:
                QtGui.QTableWidget.mousePressEvent(self,event)
    
    class MainWindow(PaintTable):
        def __init__(
            self,
            parent = None
        ):
            super(MainWindow, self).__init__(parent)
    
    # General grid
            self.table = PaintTable(self)
            self.nbrow, self.nbcol = 9, 9
            self.table.setRowCount(self.nbrow)
            self.table.setColumnCount(self.nbcol)
            for row in range(0, self.nbrow):
                self.table.setRowHeight(row, 50)
    
                for col in range(0, self.nbcol):
                    self.table.setColumnWidth(col, 50)
    
    # Each cell contains one single QTableWidgetItem
            for row in range(0, self.nbrow):
                for col in range(0, self.nbcol):
                    item = QtGui.QTableWidgetItem()
                    item.setTextAlignment(
                        QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter
                    )
    
                    self.table.setItem(row, col, item)
    
    # Header formatting
            font = QtGui.QFont()
            font.setFamily(u"DejaVu Sans")
            font.setPointSize(12)
            self.table.horizontalHeader().setFont(font)
            self.table.verticalHeader().setFont(font)
    
    # Font used
            font = QtGui.QFont()
            font.setFamily(u"DejaVu Sans")
            font.setPointSize(20)
            self.table.setFont(font)
    
    # Global Size
            self.resize(60*9, 60*9 + 20)
    
    # Layout of the table
            layout = QtGui.QGridLayout()
            layout.addWidget(self.table, 0, 0)
            self.setLayout(layout)
    
    # Set the focus in the first cell
            self.table.setFocus()
            self.table.setCurrentCell(0, 0)
    
    
    
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        fen = MainWindow()
        fen.show()
        sys.exit(app.exec_())
    

![output](http://i.stack.imgur.com/jKiaL.png)

