[PyKDE] Validating user input - PyQt
joanne matthews (RRes-Roth)
joanne.matthews at bbsrc.ac.uk
Tue Sep 5 12:35:23 BST 2006
I would like to validate user input in a table. The input needs to be a
double between 0 and 1 with 4 decimal places. I've implemented some code
to do the job and it Works fine if I create the table and set the
delegate from within the main method. However, when The table is defined
within a method in a class, the system crashes with the message
"python.exe has encountered a problem...". I'd really appreciate it if
someone could take a look. Here's my code below:
import sys
from PyQt4 import QtCore, QtGui
class CropTransitions(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.matrixTable=self.createTable(3,3)
mainLayout = QtGui.QVBoxLayout()
tableLayout=QtGui.QHBoxLayout()
tableLayout.addWidget(self.matrixTable)
mainLayout.addLayout(tableLayout)
self.setLayout(mainLayout)
def createTable(self,col,row=None):
table = QtGui.QTableWidget(row, col)
table.setShowGrid(True)
palette = QtGui.QPalette()
palette.setColor(QtGui.QPalette.Active,QtGui.QPalette.ColorRole(16),QtGu
i.QColor(204,0,0))
table.setAlternatingRowColors(1)
table.setSelectionBehavior(QtGui.QAbstractItemView.SelectionBehavior(1))
table.setSelectionMode(QtGui.QAbstractItemView.SelectionMode(1))
table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
delegate = lineEditDelegate()
table.setItemDelegate(delegate)
return table
class lineEditDelegate(QtGui.QItemDelegate):
def __init__(self, parent = None):
QtGui.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
editor = QtGui.QLineEdit(parent)
editor.setInputMask("0.0000")
editor.setCursorPosition(0)
return editor
def setEditorData(self, lineEdit, index):
value = index.model().data(index,
QtCore.Qt.DisplayRole).toString()
print "value=",value
lineEdit.setText(value)
def setModelData(self, lineEdit, model, index):
value = lineEdit.text()
model.setData(index, QtCore.QVariant(value))
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
dialog = CropTransitions()
dialog.show()
sys.exit(app.exec_())
More information about the PyQt
mailing list