[PyKDE] How to add a Table Validator

Jim Bublitz jbublitz at nwinternet.com
Wed Feb 2 05:31:41 GMT 2005


On Tuesday 01 February 2005 18:44, Steve Taetzsch wrote:
> I'm trying to port a Tkinter application to pyqt. It
> has a table that the user can enter dates in one
> column and I'd like to add a validator to the entry.
> I'd also would like to validate dollar amount entries
> in another column. After reading the docs, I'm still
> missing something. Below is some code I am trying. If
> I uncomment the lines creating an editor, the app
> slows way down. There can be a few hundred rows. I'm
> also trying to change the background color of
> alternate rows which seems to be working.
>
> Something tells me I'm doing this all wrong. I'm new
> to  qt, and am trying to decide between qt,gtk and wx
> for my new widget library. Any tips would be
> appreciated.

> dateValidRegExp is a global variable for now.

> class myTableItem(QTableItem):
>     def __init__(self, table, edittype,
> text,row,debit_flag=0):
>         QTableItem.__init__(self, table, edittype,
> text)
>         self.row = row
>         self.debit_flag = debit_flag
>
> ##        self.editor = self.createEditor()
> ##        dateValidator =
> QRegExpValidator(self.editor)
> ##        dateValidator.setRegExp(dateValidRegExp)
> ##        self.editor.setValidator(dateValidator)
>
>     def paint(self, painter, colorgroup, rect,
> selected):
>         cg = QColorGroup(colorgroup)
>         if self.row % 2:
>             cg.setColor(QColorGroup.Base,
> TABLE_GREEN_BG)
>
>
>         QTableItem.paint(self,painter, cg,rect,selected)

There's two ways I can see to go about it:

1. A single QTableItem subclass that knows (from column number or some 
column-typing mechanism you create) which validator  or custom validation 
method to use.

2. A QTable level mechanism that allows you to specify different QTableItem 
subclasses for different columns, each subclass with it's own validation 
mechanism.

Either way, you can provide different editors for different data types too 
(and "editors" can be whatever widget(s) you can fit in a cell)

Then you can either:

A. Overload the QTableItem's createEditor method and create the required 
editor with appropriate validation attached there (either different 
validators for method 1 depending on the column or a specific validator for 
method 2).

You might need to overload setContentFromEditor too if you use something other 
than a basic QLineEdit.

B. Overload 1, 2, or all of the QTableItem's __init__, setText, and 
setContentFromEditor methods depending on how you change the cell's value and 
how much you trust the source that provides the new value. Call a validator 
from each of those methods that requires validation. 

setContentFromEditor is called when editing is done to transfer the value from 
the cell's "editor" to the cell's QTableItem. 

Probably have to write your own validation code to do it this way.

C. Do your stuff in QTable.createEditor, which for some reason (I don't recall 
why) is what I did. Probably because of the way I wrote the different 
QTableItem subclasses and associated "editors".



I had a lot of tables that used different mixes of different column data 
types, so I created a column typing mechanism and overloaded QTable to 
automatically provide the right QTableItem subclass for each cell and create 
specific custom editors (the type is specified when creating the columns, 
along with col width alignment). For an app with only one or two tables, that 
probably isn't worth it.

I also used KDE/PyKDE's calendar popup stuff for date entry, so no validation 
of dates was needed.

I've rewritten this once or twice and it still seems clear as mud, so please 
don't hesitate to ask for clarification. I don't have any sample code 
available that would be useful.

Jim




More information about the PyQt mailing list