[PyQt] KeyPressEvent and Table
Christophe BAL
projetmbc at gmail.com
Sat Mar 16 09:03:41 GMT 2013
Hello,
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 *keyevent* method. This does not work : only the enter key is
recognized.
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.
One precision : some celle cannot be changed (this is for a sudoku).
Best regards.
Christophe BAL
*==== THE CODE ====*
*
*
*#! /usr/bin/env python2.7*
*# -*- coding: utf-8 -*-*
*
*
*# Source :
http://www.developpez.net/forums/d1281788/autres-langages/python-zope/gui/pyside-pyqt/coquille-vide-sudoku/#post7181230
*
*
*
*from __future__ import division*
*
*
*import sys*
*from copy import deepcopy*
*
*
*from PySide import QtCore, QtGui*
*
*
*def cellColor(painter, option, color):*
* """*
* ???*
* """*
* r = option.rect*
* x, y, w, h = r.x() + 1, r.y() + 1, r.width()-2, r.height()-2*
*
*
* if isinstance(color, (str, unicode)):*
* color = QtGui.QColor(color)*
*
*
* elif isinstance(color, (list, tuple)):*
* if len(color) == 3:*
* r, g, b = color*
* alpha = 255*
*
*
* else:*
* r, g, b, alpha = color*
*
*
* color = QtGui.QColor(r, g, b, alpha)*
*
*
* painter.fillRect(x, y, w, h, color)*
*
*
*def cellBorder(painter, option, where):*
* """*
* ???*
* """*
* r = option.rect*
* x, y, w, h = r.x(), r.y(), r.width(), r.height()*
*
*
* if where == 'up':*
* x1, y1, x2, y2 = x, y, x + w, y*
*
*
* elif where == 'bottom':*
* x1, y1, x2, y2 = x + w, y + h, x, y + h*
*
*
* elif where == 'right':*
* x1, y1, x2, y2 = x + w, y, x + w, y + h*
*
*
* elif where == 'left':*
* x1, y1, x2, y2 = x, y + h, x, y*
*
*
* else:*
* return None*
*
*
* pen = QtGui.QPen()*
* pen.setWidth(4)*
* painter.setPen(pen)*
* painter.drawLine(x1, y1, x2, y2)*
*
*
*
*
*class SudokuDelegate(QtGui.QItemDelegate):*
* def __init__(self, parent=None):*
* super(SudokuDelegate, self).__init__(parent)*
*
*
* def initGrid(self, grid):*
* self.grid_0 = grid*
*
*
* def paint(self, painter, option, index):*
* """*
* ???*
* """*
* row, col = index.row(), index.column()*
*
*
* if row == 0 or row == 3 or row == 6:*
* if col in[0,3,6]:*
* cellBorder(painter, option, 'left')*
* cellBorder(painter, option, 'up')*
*
*
* elif col == 8:*
* cellBorder(painter, option, 'right')*
* cellBorder(painter, option, 'up')*
*
*
* else:*
* cellBorder(painter, option, 'up')*
*
*
* elif row in [1,2,4,5,7]:*
* if col in [0,3,6]:*
* cellBorder(painter, option, 'left')*
*
*
* elif col == 8:*
* cellBorder(painter, option, 'right')*
*
*
* elif row == 8:*
* if col in[0,3,6]:*
* cellBorder(painter, option, 'left')*
* cellBorder(painter, option, 'bottom')*
*
*
* elif col == 8:*
* cellBorder(painter, option, 'right')*
* cellBorder(painter, option, 'bottom')*
*
*
* else:*
* cellBorder(painter, option, 'bottom')*
*
*
*# Background color for initial known cells*
* if self.grid_0[row][col]!=0:*
* cellColor(painter, option, [200,200,200])*
*
*
* QtGui.QItemDelegate.paint(self, painter, option, index)*
*
*
*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)*
*
*
*# Initial grid (to test)*
* self.grid_0 = [*
* [0,0,0, 0,2,0, 9,0,1],*
* [0,0,0, 0,0,0, 0,0,3],*
* [0,8,0, 3,0,0, 4,5,0],*
*#*
* [0,4,7, 0,0,5, 0,8,0],*
* [0,0,0, 0,0,0, 0,0,0],*
* [0,2,0, 9,0,0, 7,4,0],*
*#*
* [0,9,5, 0,0,2, 0,3,0],*
* [6,0,0, 0,0,8, 0,0,0],*
* [7,0,4, 0,6,0, 0,0,0]*
* ]*
*
*
*# Use of the delegate so to use thicker lines.*
* self.delegate = SudokuDelegate(self.table)*
* self.table.setItemDelegate(self.delegate)*
* self.delegate.initGrid(self.grid_0)*
*
*
*# Initialization of the actual grid*
* self.grid = deepcopy(self.grid_0)*
*
*
*# Display the grid.*
* self.update(self.grid)*
*
*
*# Set the focus in the first cell*
* self.table.setFocus()*
* self.table.setCurrentCell(0, 0)*
*
*
* def update(self, g):*
* for row in range(0, len(g[0])):*
* for col in range(0, len(g)):*
* if g[row][col] == 0:*
* font = QtGui.QFont()*
* font.setFamily(u"DejaVu Sans")*
* font.setPointSize(12)*
* self.table.item(row, col).setFont(font)*
*
*
* color = QtGui.QColor(0, 0, 255, 255) # bleu*
* self.table.item(row, col).setForeground(color)*
*
*
* self.table.item(row, col).setText(*
* u"1 2 3\n4 5 6\n7 8 9"*
* )*
* self.table.item(*
* row, col*
* ).setFlags(*
* QtCore.Qt.ItemIsEnabled*
* | QtCore.Qt.ItemIsSelectable*
*# | QtCore.Qt.ItemIsEditable*
* )*
*
*
* else:*
* self.table.item(row, col).setText(unicode(g[row][col]))
*
* self.table.item(*
* row, col*
* ).setFlags(*
* QtCore.Qt.ItemIsEnabled*
* | QtCore.Qt.ItemIsSelectable*
* )*
*
*
*# Just for testing !*
* color = QtGui.QColor(160, 255, 160, 255) # Light green*
* self.table.item(2, 4).setBackground(color)*
*
*
* color = QtGui.QColor(255, 160, 160, 255) # Light red*
* self.table.item(6, 3).setBackground(color)*
*
*
* def keyPressEvent(self, event):*
* if type(event) == QtGui.QKeyEvent:*
* #here accept the event and do something*
* print event.key()*
* event.accept()*
* else:*
* event.ignore()*
*
*
*# print self.table.currentColumn(), self.table.currentRow()*
*
*
*
*
*if __name__ == "__main__":*
* app = QtGui.QApplication(sys.argv)*
* fen = MainWindow()*
* fen.show()*
* sys.exit(app.exec_())*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20130316/f443412e/attachment-0001.html>
More information about the PyQt
mailing list