[PyKDE] Problem with dynamic tooltip

stsao stsao at eudoramail.com
Sat Nov 24 19:01:32 GMT 2001


When I run the following code, I get a GPF.   This occurs with both PyQT 
2.5 and PyQt 3.0.
I am running Python 2.0 on Windows 2000 and PyQT was build using VC++ 6.0.

If you USE_DYNAMIC_TOOLTIP = false, then the code runs fine.  This code is 
basically a Python port of the example tooltip.cpp code shipped with Qt 
(which runs fine)

Can someone confirm that this indeed a problem and not just a local built 
error?

Thanks
Sheng-Te Tsao
========================================================

from whrandom import randint
import qt
from qt import QApplication, QObject, SLOT, SIGNAL, PYSIGNAL
from qt import QPainter, QToolTip, QRect, QWidget, Qt


class DynamicTip(QToolTip):

     def __init__(self, parent):
         # no explicit initialization needed
         print "DynamicTip.__init__()"
         QToolTip.__init__(self, parent)
         assert parent


     def maybeTip(self, point):
         assert point
         if not isinstance(self.parentWidget(), TellMe):
             return
         print "maybeTip(%d, %d)" % (point.x(), point.y())
         rect = parentWidget().tip(point)
         if not rect:
             return
         print "rect(%d, %d)" % (rect.center().x(),rect.center().y())
         self.tip(rect, "pos: %d,%d" % (rect.center().x(),rect.center().y()))


class TellMe(QWidget):

     USE_DYNAMIC_TOOLTIP = true

     def __init__(self, parent = None , name = ""):
         QWidget.__init__(self, parent, name)
         self.setMinimumSize( 30, 30 )
         self.r1 = self.randomRectAndTooltip()
         self.r2 = self.randomRectAndTooltip()
         self.r3 = self.randomRect()
         if TellMe.USE_DYNAMIC_TOOLTIP:
             # Currently this causes a GPF
             tooltip = DynamicTip(self)
         QToolTip.add(self, self.r3, "this color is called red")  # <- helpful


     def randomRectAndTooltip(self):
         rect = self.randomRect()
         if not TellMe.USE_DYNAMIC_TOOLTIP:
             QToolTip.add(self, rect,
                          "pos: %d,%d" % (rect.center().x(),rect.center().y()))
         return rect


     def paintEvent(self, event):
         painter = QPainter(self)
         rect = event.rect()
         # I try to be efficient here, and repaint only what's needed
         if rect.intersects(self.r1):
             painter.setBrush(Qt.blue)
             painter.drawRect(self.r1)
         if rect.intersects(self.r2):
             painter.setBrush(Qt.blue)
             painter.drawRect(self.r2)
         if rect.intersects(self.r3):
             painter.setBrush(Qt.red)
             painter.drawRect(self.r3)


     def mousePressEvent(self, event):
         point = event.pos()
         if self.r1.contains(point):
             self.r1 = self.randomRectAndTooltip()
         if self.r2.contains(point):
             self.r2 = self.randomRectAndTooltip()
         self.repaint()


     def resizeEvent(self, event):
         rect = self.rect()
         if not rect.contains(self.r1):
             self.r1 = self.randomRectAndTooltip()
         if not rect.contains(self.r2):
             self.r2 = self.randomRectAndTooltip()


     def randomRect(self):
         return QRect(randint(20, (self.width() - 20)),
                      randint(20, (self.height() - 20)),
                      20, 20)


     def tip(self, point):
         if self.r1.contains(point):
             return self.r1
         elif self.r2.contains(point):
             return self.r2
         else:
             return None


if __name__ == "__main__":

     def main(argv):
         app = QApplication(argv)
         mainWidget = TellMe()
         app.setMainWidget(mainWidget)
         mainWidget.setCaption( "Dynamic Tool Tip Demonstration" );
         mainWidget.show();
         app.exec_loop()


     import sys
     main(sys.argv)

Sheng-Te Tsao
Online Status: 
<http://eudora.voicecontact.com/vc3/?tst%40eudoramail.com><http://eudora.voicecontact.com/vc3/?tst%40eudoramail.com><http://www.eudora.com/products/voicecontact/>






More information about the PyQt mailing list