Hello,<div>I would like to act before the change of the content of one cell in a table. To do that I have made all the cells not editable, and I have tried to use the <b><font face="courier new, monospace">keyevent</font></b> method. This does not work : only the enter key is recognized. </div>
<div><br></div><div>Indeed, I would to act if A is pressed, without changing the content of the cell, and also to change the content only if one digit is pressed. What is the good way to do that ? I've put one minimal not working code just after. </div>
<div><br></div><div>One precision : some celle cannot be changed (this is for a sudoku).</div><div><br></div><div>Best regards.</div><div>Christophe BAL<br><div><br></div><div><br></div><div><br></div><div><font face="courier new, monospace"><b>==== THE CODE ====</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><div><font face="courier new, monospace"><b>#! /usr/bin/env python2.7</b></font></div><div><font face="courier new, monospace"><b># -*- coding: utf-8 -*-</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b># Source : <a href="http://www.developpez.net/forums/d1281788/autres-langages/python-zope/gui/pyside-pyqt/coquille-vide-sudoku/#post7181230">http://www.developpez.net/forums/d1281788/autres-langages/python-zope/gui/pyside-pyqt/coquille-vide-sudoku/#post7181230</a></b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b>from __future__ import division</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div>
<div><font face="courier new, monospace"><b>import sys</b></font></div><div><font face="courier new, monospace"><b>from copy import deepcopy</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div>
<div><font face="courier new, monospace"><b>from PySide import QtCore, QtGui</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b>def cellColor(painter, option, color):</b></font></div>
<div><font face="courier new, monospace"><b> """</b></font></div><div><font face="courier new, monospace"><b> ???</b></font></div><div><font face="courier new, monospace"><b> """</b></font></div>
<div><font face="courier new, monospace"><b> r = option.rect</b></font></div><div><font face="courier new, monospace"><b> x, y, w, h = r.x() + 1, r.y() + 1, r.width()-2, r.height()-2</b></font></div><div><font face="courier new, monospace"><b><br>
</b></font></div><div><font face="courier new, monospace"><b> if isinstance(color, (str, unicode)):</b></font></div><div><font face="courier new, monospace"><b> color = QtGui.QColor(color)</b></font></div><div><font face="courier new, monospace"><b><br>
</b></font></div><div><font face="courier new, monospace"><b> elif isinstance(color, (list, tuple)):</b></font></div><div><font face="courier new, monospace"><b> if len(color) == 3:</b></font></div><div><font face="courier new, monospace"><b> r, g, b = color</b></font></div>
<div><font face="courier new, monospace"><b> alpha = 255</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> else:</b></font></div>
<div><font face="courier new, monospace"><b> r, g, b, alpha = color</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> color = QtGui.QColor(r, g, b, alpha)</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> painter.fillRect(x, y, w, h, color)</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div>
<div><font face="courier new, monospace"><b>def cellBorder(painter, option, where):</b></font></div><div><font face="courier new, monospace"><b> """</b></font></div><div><font face="courier new, monospace"><b> ???</b></font></div>
<div><font face="courier new, monospace"><b> """</b></font></div><div><font face="courier new, monospace"><b> r = option.rect</b></font></div><div><font face="courier new, monospace"><b> x, y, w, h = r.x(), r.y(), r.width(), r.height()</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> if where == 'up':</b></font></div><div><font face="courier new, monospace"><b> x1, y1, x2, y2 = x, y, x + w, y</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> elif where == 'bottom':</b></font></div><div><font face="courier new, monospace"><b> x1, y1, x2, y2 = x + w, y + h, x, y + h</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> elif where == 'right':</b></font></div><div><font face="courier new, monospace"><b> x1, y1, x2, y2 = x + w, y, x + w, y + h</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> elif where == 'left':</b></font></div><div><font face="courier new, monospace"><b> x1, y1, x2, y2 = x, y + h, x, y</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> else:</b></font></div><div><font face="courier new, monospace"><b> return None</b></font></div><div><font face="courier new, monospace"><b><br>
</b></font></div><div><font face="courier new, monospace"><b> pen = QtGui.QPen()</b></font></div><div><font face="courier new, monospace"><b> pen.setWidth(4)</b></font></div><div><font face="courier new, monospace"><b> painter.setPen(pen)</b></font></div>
<div><font face="courier new, monospace"><b> painter.drawLine(x1, y1, x2, y2)</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b><br></b></font></div>
<div><font face="courier new, monospace"><b>class SudokuDelegate(QtGui.QItemDelegate):</b></font></div><div><font face="courier new, monospace"><b> def __init__(self, parent=None):</b></font></div><div><font face="courier new, monospace"><b> super(SudokuDelegate, self).__init__(parent)</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> def initGrid(self, grid):</b></font></div><div><font face="courier new, monospace"><b> self.grid_0 = grid</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> def paint(self, painter, option, index):</b></font></div><div><font face="courier new, monospace"><b> """</b></font></div>
<div><font face="courier new, monospace"><b> ???</b></font></div><div><font face="courier new, monospace"><b> """</b></font></div><div><font face="courier new, monospace"><b> row, col = index.row(), index.column()</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> if row == 0 or row == 3 or row == 6:</b></font></div><div><font face="courier new, monospace"><b> if col in[0,3,6]:</b></font></div>
<div><font face="courier new, monospace"><b> cellBorder(painter, option, 'left')</b></font></div><div><font face="courier new, monospace"><b> cellBorder(painter, option, 'up')</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> elif col == 8:</b></font></div><div><font face="courier new, monospace"><b> cellBorder(painter, option, 'right')</b></font></div>
<div><font face="courier new, monospace"><b> cellBorder(painter, option, 'up')</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> else:</b></font></div>
<div><font face="courier new, monospace"><b> cellBorder(painter, option, 'up')</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> elif row in [1,2,4,5,7]:</b></font></div>
<div><font face="courier new, monospace"><b> if col in [0,3,6]:</b></font></div><div><font face="courier new, monospace"><b> cellBorder(painter, option, 'left')</b></font></div><div><font face="courier new, monospace"><b><br>
</b></font></div><div><font face="courier new, monospace"><b> elif col == 8:</b></font></div><div><font face="courier new, monospace"><b> cellBorder(painter, option, 'right')</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> elif row == 8:</b></font></div><div><font face="courier new, monospace"><b> if col in[0,3,6]:</b></font></div>
<div><font face="courier new, monospace"><b> cellBorder(painter, option, 'left')</b></font></div><div><font face="courier new, monospace"><b> cellBorder(painter, option, 'bottom')</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> elif col == 8:</b></font></div><div><font face="courier new, monospace"><b> cellBorder(painter, option, 'right')</b></font></div>
<div><font face="courier new, monospace"><b> cellBorder(painter, option, 'bottom')</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> else:</b></font></div>
<div><font face="courier new, monospace"><b> cellBorder(painter, option, 'bottom')</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b># Background color for initial known cells</b></font></div>
<div><font face="courier new, monospace"><b> if self.grid_0[row][col]!=0:</b></font></div><div><font face="courier new, monospace"><b> cellColor(painter, option, [200,200,200])</b></font></div><div><font face="courier new, monospace"><b><br>
</b></font></div><div><font face="courier new, monospace"><b> QtGui.QItemDelegate.paint(self, painter, option, index)</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b>class MainWindow(QtGui.QWidget):</b></font></div>
<div><font face="courier new, monospace"><b> def __init__(</b></font></div><div><font face="courier new, monospace"><b> self,</b></font></div><div><font face="courier new, monospace"><b> parent = None</b></font></div>
<div><font face="courier new, monospace"><b> ):</b></font></div><div><font face="courier new, monospace"><b> super(MainWindow, self).__init__(parent)</b></font></div><div><font face="courier new, monospace"><b><br>
</b></font></div><div><font face="courier new, monospace"><b># General grid</b></font></div><div><font face="courier new, monospace"><b> self.table = QtGui.QTableWidget(self)</b></font></div><div><font face="courier new, monospace"><b> self.nbrow, self.nbcol = 9, 9</b></font></div>
<div><font face="courier new, monospace"><b> self.table.setRowCount(self.nbrow)</b></font></div><div><font face="courier new, monospace"><b> self.table.setColumnCount(self.nbcol)</b></font></div><div><font face="courier new, monospace"><b><br>
</b></font></div><div><font face="courier new, monospace"><b># Each cell has dimension 50 pixels x 50 pixels</b></font></div><div><font face="courier new, monospace"><b> for row in range(0, self.nbrow):</b></font></div>
<div><font face="courier new, monospace"><b> self.table.setRowHeight(row, 50)</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> for col in range(0, self.nbcol):</b></font></div>
<div><font face="courier new, monospace"><b> self.table.setColumnWidth(col, 50)</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b># Each cell contains one single QTableWidgetItem</b></font></div>
<div><font face="courier new, monospace"><b> for row in range(0, self.nbrow):</b></font></div><div><font face="courier new, monospace"><b> for col in range(0, self.nbcol):</b></font></div><div><font face="courier new, monospace"><b> item = QtGui.QTableWidgetItem()</b></font></div>
<div><font face="courier new, monospace"><b> item.setTextAlignment(</b></font></div><div><font face="courier new, monospace"><b> QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter</b></font></div>
<div><font face="courier new, monospace"><b> )</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> self.table.setItem(row, col, item)</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b># Header formatting</b></font></div><div><font face="courier new, monospace"><b> font = QtGui.QFont()</b></font></div>
<div><font face="courier new, monospace"><b> font.setFamily(u"DejaVu Sans")</b></font></div><div><font face="courier new, monospace"><b> font.setPointSize(12)</b></font></div><div><font face="courier new, monospace"><b> self.table.horizontalHeader().setFont(font)</b></font></div>
<div><font face="courier new, monospace"><b> self.table.verticalHeader().setFont(font)</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b># Font used</b></font></div>
<div><font face="courier new, monospace"><b> font = QtGui.QFont()</b></font></div><div><font face="courier new, monospace"><b> font.setFamily(u"DejaVu Sans")</b></font></div><div><font face="courier new, monospace"><b> font.setPointSize(20)</b></font></div>
<div><font face="courier new, monospace"><b> self.table.setFont(font)</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b># Global Size</b></font></div>
<div><font face="courier new, monospace"><b> self.resize(60*9, 60*9 + 20)</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b># Layout of the table</b></font></div>
<div><font face="courier new, monospace"><b> layout = QtGui.QGridLayout()</b></font></div><div><font face="courier new, monospace"><b> layout.addWidget(self.table, 0, 0)</b></font></div><div><font face="courier new, monospace"><b> self.setLayout(layout)</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b># Initial grid (to test)</b></font></div><div><font face="courier new, monospace"><b> self.grid_0 = [</b></font></div>
<div><font face="courier new, monospace"><b> [0,0,0, 0,2,0, 9,0,1],</b></font></div><div><font face="courier new, monospace"><b> [0,0,0, 0,0,0, 0,0,3],</b></font></div><div><font face="courier new, monospace"><b> [0,8,0, 3,0,0, 4,5,0],</b></font></div>
<div><font face="courier new, monospace"><b>#</b></font></div><div><font face="courier new, monospace"><b> [0,4,7, 0,0,5, 0,8,0],</b></font></div><div><font face="courier new, monospace"><b> [0,0,0, 0,0,0, 0,0,0],</b></font></div>
<div><font face="courier new, monospace"><b> [0,2,0, 9,0,0, 7,4,0],</b></font></div><div><font face="courier new, monospace"><b>#</b></font></div><div><font face="courier new, monospace"><b> [0,9,5, 0,0,2, 0,3,0],</b></font></div>
<div><font face="courier new, monospace"><b> [6,0,0, 0,0,8, 0,0,0],</b></font></div><div><font face="courier new, monospace"><b> [7,0,4, 0,6,0, 0,0,0]</b></font></div><div><font face="courier new, monospace"><b> ]</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b># Use of the delegate so to use thicker lines.</b></font></div><div><font face="courier new, monospace"><b> self.delegate = SudokuDelegate(self.table)</b></font></div>
<div><font face="courier new, monospace"><b> self.table.setItemDelegate(self.delegate)</b></font></div><div><font face="courier new, monospace"><b> self.delegate.initGrid(self.grid_0)</b></font></div><div><font face="courier new, monospace"><b><br>
</b></font></div><div><font face="courier new, monospace"><b># Initialization of the actual grid</b></font></div><div><font face="courier new, monospace"><b> self.grid = deepcopy(self.grid_0)</b></font></div><div><font face="courier new, monospace"><b><br>
</b></font></div><div><font face="courier new, monospace"><b># Display the grid.</b></font></div><div><font face="courier new, monospace"><b> self.update(self.grid)</b></font></div><div><font face="courier new, monospace"><b><br>
</b></font></div><div><font face="courier new, monospace"><b># Set the focus in the first cell</b></font></div><div><font face="courier new, monospace"><b> self.table.setFocus()</b></font></div><div><font face="courier new, monospace"><b> self.table.setCurrentCell(0, 0)</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> def update(self, g):</b></font></div><div><font face="courier new, monospace"><b> for row in range(0, len(g[0])):</b></font></div>
<div><font face="courier new, monospace"><b> for col in range(0, len(g)):</b></font></div><div><font face="courier new, monospace"><b> if g[row][col] == 0:</b></font></div><div><font face="courier new, monospace"><b> font = QtGui.QFont()</b></font></div>
<div><font face="courier new, monospace"><b> font.setFamily(u"DejaVu Sans")</b></font></div><div><font face="courier new, monospace"><b> font.setPointSize(12)</b></font></div>
<div><font face="courier new, monospace"><b> self.table.item(row, col).setFont(font)</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> color = QtGui.QColor(0, 0, 255, 255) # bleu</b></font></div>
<div><font face="courier new, monospace"><b> self.table.item(row, col).setForeground(color)</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> self.table.item(row, col).setText(</b></font></div>
<div><font face="courier new, monospace"><b> u"1 2 3\n4 5 6\n7 8 9"</b></font></div><div><font face="courier new, monospace"><b> )</b></font></div><div><font face="courier new, monospace"><b> self.table.item(</b></font></div>
<div><font face="courier new, monospace"><b> row, col</b></font></div><div><font face="courier new, monospace"><b> ).setFlags(</b></font></div><div><font face="courier new, monospace"><b> QtCore.Qt.ItemIsEnabled</b></font></div>
<div><font face="courier new, monospace"><b> | QtCore.Qt.ItemIsSelectable</b></font></div><div><font face="courier new, monospace"><b># | QtCore.Qt.ItemIsEditable</b></font></div>
<div><font face="courier new, monospace"><b> )</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> else:</b></font></div>
<div><font face="courier new, monospace"><b> self.table.item(row, col).setText(unicode(g[row][col]))</b></font></div><div><font face="courier new, monospace"><b> self.table.item(</b></font></div>
<div><font face="courier new, monospace"><b> row, col</b></font></div><div><font face="courier new, monospace"><b> ).setFlags(</b></font></div><div><font face="courier new, monospace"><b> QtCore.Qt.ItemIsEnabled</b></font></div>
<div><font face="courier new, monospace"><b> | QtCore.Qt.ItemIsSelectable</b></font></div><div><font face="courier new, monospace"><b> )</b></font></div><div><font face="courier new, monospace"><b><br>
</b></font></div><div><font face="courier new, monospace"><b># Just for testing !</b></font></div><div><font face="courier new, monospace"><b> color = QtGui.QColor(160, 255, 160, 255) # Light green</b></font></div>
<div><font face="courier new, monospace"><b> self.table.item(2, 4).setBackground(color)</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> color = QtGui.QColor(255, 160, 160, 255) # Light red</b></font></div>
<div><font face="courier new, monospace"><b> self.table.item(6, 3).setBackground(color)</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b> def keyPressEvent(self, event):</b></font></div>
<div><font face="courier new, monospace"><b> if type(event) == QtGui.QKeyEvent:</b></font></div><div><font face="courier new, monospace"><b> #here accept the event and do something</b></font></div><div>
<font face="courier new, monospace"><b> print event.key()</b></font></div><div><font face="courier new, monospace"><b> event.accept()</b></font></div><div><font face="courier new, monospace"><b> else:</b></font></div>
<div><font face="courier new, monospace"><b> event.ignore()</b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b># print self.table.currentColumn(), self.table.currentRow()</b></font></div>
<div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b><br></b></font></div><div><font face="courier new, monospace"><b>if __name__ == "__main__":</b></font></div>
<div><font face="courier new, monospace"><b> app = QtGui.QApplication(sys.argv)</b></font></div><div><font face="courier new, monospace"><b> fen = MainWindow()</b></font></div><div><font face="courier new, monospace"><b> fen.show()</b></font></div>
<div><font face="courier new, monospace"><b> sys.exit(app.exec_())</b></font></div></div><div><br></div></div>