SIP4 python bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tooltip.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. """**************************************************************************
  3. ** $Id: tooltip.py,v 1.1 2003/07/01 14:18:37 phil Exp $
  4. **
  5. ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
  6. **
  7. ** This file is part of an example program for TQt. This example
  8. ** program may be used, distributed and modified without limitation.
  9. **
  10. ***************************************************************************"""
  11. import sys
  12. from python_tqt.qt import *
  13. from random import random
  14. class DynamicTip( TQToolTip ):
  15. def __init__( self, parent ):
  16. TQToolTip.__init__( self, parent )
  17. def maybeTip( self, pos ):
  18. #if not self.parent.inherits( "TellMe" ):
  19. if TQToolTip(self).parentWidget().inherits( "TellMe" ) :
  20. return
  21. r = TQRect( TQToolTip(self).parentWidget().tip(pos) )
  22. if not r.isValid():
  23. return
  24. s = TQString( "position: %d,%d" % (r.center().x(), r.center().y()) )
  25. TQToolTip(self).tip( r, s )
  26. class TellMe( TQWidget ):
  27. def __init__( self, parent=None, name=None ):
  28. TQWidget.__init__( self, parent, name )
  29. self.setMinimumSize( 30, 30 )
  30. self.r1 = self.randomRect()
  31. self.r2 = self.randomRect()
  32. self.r3 = self.randomRect()
  33. self.t = DynamicTip( self )
  34. TQToolTip.add( self, self.r3, "this color is called red" ) # <- helpful
  35. def paintEvent( self, e ):
  36. p = TQPainter( self )
  37. # I try to be efficient here, and repaint only what's needed
  38. if e.rect().intersects( self.r1 ):
  39. p.setBrush( TQt.blue )
  40. p.drawRect( self.r1 )
  41. if e.rect().intersects( self.r2 ):
  42. p.setBrush( TQt.blue )
  43. p.drawRect( self.r2 )
  44. if e.rect().intersects( self.r3 ):
  45. p.setBrush( TQt.red )
  46. p.drawRect( self.r3 )
  47. def mousePressEvent( self, e ):
  48. if self.r1.contains( e.pos() ):
  49. self.r1 = self.randomRect()
  50. if self.r2.contains( e.pos() ):
  51. self.r2 = self.randomRect()
  52. self.repaint()
  53. def resizeEvent( self, e ):
  54. if not self.rect().contains( self.r1 ):
  55. self.r1 = self.randomRect()
  56. if not self.rect().contains( self.r2 ):
  57. self.r2 = self.randomRect()
  58. def randomRect( self ):
  59. return TQRect( random() * (self.width() - 20), random() * (self.height() - 20), 20, 20 )
  60. def tip( self, p ):
  61. if self.r1.contains( p ):
  62. return self.r1
  63. elif self.r2.contains( p ):
  64. return self.r2
  65. else:
  66. return TQRect( 0,0, -1,-1 )
  67. def __del__( self ):
  68. del self.t
  69. self.t = None
  70. def main( args ):
  71. a = TQApplication( args )
  72. mw = TellMe()
  73. mw.setCaption( "TQt Example - Dynamic Tool Tips" )
  74. a.setMainWidget( mw )
  75. mw.show()
  76. a.exec_loop()
  77. if __name__=="__main__":
  78. main(sys.argv)