code: def mousePressEvent(self, event):
          
          if event.button() != Qt.LeftButton:
              QTableView.mousePressEvent(self, event)
              return
          index_clicked = self.indexAt(event.pos())
          if index_clicked.isValid():
              if index_clicked == self.currentIndex() \
                 and index_clicked in self.selectedIndexes():
                  self.clearSelection()
              else:
                  QTableView.mousePressEvent(self, event)
          else:
              self.clearSelection()
              event.accept()
docs: Reimplement Qt method