[PyKDE] question re python interface to QPixmapCache

Tony Willis Tony.Willis at nrc-cnrc.gc.ca
Wed Mar 9 19:15:14 GMT 2005


Hi

I am trying to translate the 'traffic lights' example of the
use of QPixmaps and QPixmapCache in Mark Summerfield's
article in Qt Quarterly
(http://doc.trolltech.com/qq/qq12-qpixmapcache.html)
to python. My version is given at the end of this message.

The appended code almost works, but the call to
QPixmapCache in line 64 of the code croaks
when a pixmap and its corresponding key have
previously been inserted into the QPixmapCache. Am I doing
something incredibly silly, or is there an
actual problem with the python interface to QPixmapCache?

Note: the esssentially equivalent code (commented out)
that uses a standard python dict to store the pixmaps
works fine.

System: RH9 linux
        python 2.3.4
        PyQt-x11-gpl-3.12
        sip 4.0.1

Thanks for any advice. Could you please copy me by e-mail
directly, as I am not a susbscriber to the PyKde interest
group.

Thanks

Tony
___________
Tony Willis
National Research Council   Tony.Willis at nrc-cnrc.gc.ca
Box 248                     (250)493-2277
Penticton, BC  V2A 6J9      fax: 493-7767
Government of Canada        Gouvernement du Canada

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#!/usr/bin/env python

# python adaption of the c++ 'traffic lights' widget discussed
# in the article 'Optimizing with QPixmapCache' by Mark Summerfield
# in 'Qt Quarterly' Issue 12, Q4 2004

import sys, math
from qt import *

class Lights(QWidget):

  def __init__(self, *args):
    QWidget.__init__(self, *args)

    self.m_color = Qt.red
    self.m_diameter = 80
    self.pixmap_cache = {}

  def color(self):
    return self.m_color

  def diameter(self):
    return self.m_diameter

  def sizeHint(self):
    return QSize(self.m_diameter, 3 * self.m_diameter)

  def setDiameter(self, diameter):
    self.m_diameter = diameter
    self.update()
    self.updateGeometry()

  def setColor(self, newColor):
    self.m_color = newColor
    self.update()
    self.updateGeometry()

  def mousePressEvent(self, event):
    if event.y() < self.m_diameter:
       self.m_color = Qt.red
    elif event.y() < 2 * self.m_diameter:
       self.m_color = Qt.yellow
    else:
       self.m_color = Qt.green
    self.update()

  def paintEvent(self, QPaintEvent):
    key = "lights: " + str(self.m_color.name()) + ' ' + str(self.m_diameter)
####### this works
#   pixmap = None
#   if self.pixmap_cache.has_key(key) == False:
#     pixmap = self.generatePixmap()
#     self.pixmap_cache[key] =  pixmap
#   else:
#     pixmap = self.pixmap_cache[key]
#   bitBlt(self, 0, 0, pixmap)
####### this worked

####### this does not work
    pixmap = QPixmap()
    print ' '
    print 'key ', key
    print 'calling QPixmapCache.find'
    if QPixmapCache.find(key,pixmap):
      print ' '
      print 'calling bitBlt'
      bitBlt(self, 0, 0, pixmap)
      print 'called bitBlt'
    else:
      print 'creating pixmap '
      pixmap1 = self.generatePixmap()
      QPixmapCache.insert(key,pixmap1)
      print 'calling bitBlt'
      bitBlt(self, 0, 0, pixmap1)
      print 'called bitBlt'
####### this did not work

  def generatePixmap(self):

    w = self.m_diameter
    h = 3 * self.m_diameter
    pixmap = QPixmap(w,h)

    painter = QPainter(pixmap)
    painter.setBrush(Qt.darkGray)
    painter.drawRect(0,0,w,h)
    brush_color = Qt.lightGray
    if self.m_color == Qt.red:
       brush_color = self.m_color
    painter.setBrush(brush_color)
    painter.drawEllipse(0,0,w,w)
    brush_color = Qt.lightGray
    if self.m_color == Qt.yellow:
       brush_color = self.m_color
    painter.setBrush(brush_color)
    painter.drawEllipse(0,w,w,w)
    brush_color = Qt.lightGray
    if self.m_color == Qt.green:
        brush_color = self.m_color
    painter.setBrush(brush_color)
    painter.drawEllipse(0,2*w,w,w)
    return pixmap

def main(args):
    app = QApplication(args)
    demo = Lights()
    demo.show()
    app.setMainWidget(demo)
    app.exec_loop()

# Admire!
if __name__ == '__main__':
    main(sys.argv)






More information about the PyQt mailing list