[PyKDE] Speed of derived objects

Vincent Wagelaar vincent at ricardis.tudelft.nl
Tue Nov 26 00:12:01 GMT 2002


On Monday 25 November 2002 22:44, Hans-Peter Jansen wrote:

[snip]
> > Overloading QListView shouldn't give a performance degradation because
> > it's only called once. Nothing compared to how many times MyListViewItem
> > is called.
>
> I can imagine this would give a nice example. If you like to share some
> code, I'm going to prepare one.

Here an example of a listview that colours positive items green, zeros orange 
and negative values red...

from qt import *

class ColorListItem(QListViewItem):
	def __init__(self, *args):
		QListViewItem.__init__(self, *args)
	# Overridden to color changes that are positive green, negative red, and 0 
orange
	def paintCell(self, painter, qg, column, width, align):

		if column ==0:
			try:
			  pstr = float(str(self.text(column)))
			except:
			  pstr = 0
			if pstr > 0:
				#Green
				qg.setColor(QColorGroup.Text, QColor(0,155,0))
			elif pstr < 0:
				#Red
				qg.setColor(QColorGroup.Text, QColor(200,0,0))
			else:
				#Orange
				qg.setColor(QColorGroup.Text, QColor(255,100,0))

		QListViewItem.paintCell(self, painter, qg, column, width, align)
	
	def key(self, column, asc):
		if column == 0:
			try:
			  tmp = float(str(self.text(column)))
			except:
			  tmp = 0
			#A bit of a hack to sort numerical values
			return "%015d" % (tmp*100+100000)
		else:
			return QListViewItem.key(self, column, asc)

class ColorList(QListView):
	def __init__(self, *args):
		apply(QListView.__init__, (self, )+ args)

		#Set name of columns
		self.addColumn("Color")

		#Set width of columns
		self.setColumnWidth(0, 150)

		#Set alignment to the right
		self.setColumnAlignment(0, 2)

		self.setAllColumnsShowFocus(1)

		i = -100

		while i < 200:
			ColorListItem(self, str(i))
			i+=1


Hopefully this example is nice enough? I think this is problably one of the 
few cases where you need to make an C++ implementation because of speed. It 
would be nice if calling (overloaded) methods in the next version of Python 
would be speedier!! On my computer it's about ten times slower than directly 
calling QListViewItem in both populating the listview and sorting.

Cheers,
Vincent

> > > If this all don't work for you, then I'm afraid you have to implement
> > > your derived class in C++ and use sip to bind this and use it from
> > > Python.
> >
> > I'm affraid so too, but shouldn't give that many troubles. I've also
> > tried Psyco but that didn't give a speed boost either.
> >
> > Thanks for the reply
> >
> > Vincent
>
> Bye,
> Hans-Peter
>
> _______________________________________________
> PyKDE mailing list    PyKDE at mats.gmd.de
> http://mats.gmd.de/mailman/listinfo/pykde




More information about the PyQt mailing list