PyQt QTableItem inheritance...

Phil Thompson phil at river-bank.demon.co.uk
Fri Apr 4 04:22:06 EST 2003


On Wednesday 02 April 2003 7:18 pm, Mike Meyer wrote:
> Phil Thompson <phil at river-bank.demon.co.uk> writes:
> > On Tuesday 01 April 2003 5:31 pm, Mike Meyer wrote:
> > > I've already tried posting this to the pyKDE mail list, and gotten no
> > > response.
> > >
> > > I'm trying to create an item in a QTable that uses the
> > > QDoubleValidator on the input edit field. The only way I can see to do
> > > this is subclass QTableItem, and override createEditor with a method
> > > that sets the validator, like so:
>
> [code elided]
>
> > > While I get the output from __init__, I never see the output from
> > > createEditor. Any help would be appreciated.
> >
> > A small but complete example would help.
>
> The rest of it is biolerplate, but here it is.
>
>         <mike
>
> import sys
> from qt import *
> from qttable import *
>
> class Float_Field(QTableItem):
>
>     def __init__(self, table, data):
>         print "Creating float field."
>         QTableItem.__init__(self, table, QTableItem.OnTyping, str(data))
>
>     def createEditor(self):
>         print "createEditor"
>         editor = QLineEdit(self.text(), self.table().viewport())
>         self.connect(editor, SIGNAL("textChanged(QString &)"),
>                      self.table(), SLOT("doValueChanged"))
>         editor.setValidator(QDoubleValidator(self))
>         return editor
>
>
> app = QApplication(sys.argv)
> table = QTable(1, 1)
> table.setItem(1, 1, Float_Field(table, "12.34"))
> app.setMainWidget(table)
> table.show()
> app.exec_loop()

There are a number of problems here...

connect() is a method of QObject derived classes and QTableItem isn't derived 
from QObject. Use either QObject.connect() or editor.connect().

"textChanged(QString &)" is not a QTable signal - it's "textChanged(const 
QString &)".

QTable doesn't have a slot called "doValueChanged". If you are trying to 
handle the signal in Python then the call to connect() should be something 
like...

	editor.connect(editor, SIGNAL("textChanged(const QString &)"),
			self.doValueChanged)

...and implement doValueChanged() as a method of Float_Field...

	def doValueChanged(self, value):
		print "Changed value:", str(value)

The validator will not work as QLineEdit.setValidator() doesn't take ownership 
of it and it will be garbage collected immediately afterwards. Do something 
like...

	self.validator = QDoubleValidator(editor)
	editor.setValidator(self.validator)

Finally, the call to QTable.setItem() is wrong. The only valid coordinates for 
this table are (0, 0).

With those changes double-clicking on the cell will create the editor and 
validate any changes.

Attached is a corrected script.

Phil

-------------- next part --------------
A non-text attachment was scrubbed...
Name: table.py
Type: text/x-python
Size: 859 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030404/d36892bb/attachment.py>


More information about the Python-list mailing list