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.

drawlines.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python
  2. import sys, random
  3. from python_tqt.qt import *
  4. TRUE = 1
  5. FALSE = 0
  6. MAXPOINTS = 2000; # maximum number of points
  7. MAXCOLORS = 40;
  8. #
  9. # ConnectWidget - draws connected lines
  10. #
  11. class ConnectWidget(TQWidget):
  12. def __init__(self):
  13. TQWidget.__init__(self)
  14. self.setEraseColor( TQt.white ) # white background
  15. self.count = 0;
  16. self.down = FALSE
  17. self.points = []
  18. self.colors = []
  19. for i in range(MAXPOINTS): # init arrays
  20. self.points.append(TQPoint())
  21. for i in range(MAXCOLORS):
  22. self.colors.append(TQColor( random.randint(0,255), random.randint(0,255), random.randint(0,255) ))
  23. #
  24. # Handles paint events for the connect widget.
  25. #
  26. def paintEvent(self, pe):
  27. paint = TQPainter( self )
  28. for i in range(self.count-1): # connect all points
  29. for j in range(i+1, self.count):
  30. paint.setPen( self.colors[random.randint(0,MAXCOLORS-1)] ) # set random pen color
  31. paint.drawLine( self.points[i], self.points[j] ) # draw line
  32. #
  33. # Handles mouse press events for the connect widget.
  34. #
  35. def mousePressEvent(self, me):
  36. self.down = TRUE
  37. self.count = 0 # start recording points
  38. self.erase() # erase widget contents
  39. #
  40. # Handles mouse release events for the connect widget.
  41. #
  42. def mouseReleaseEvent(self, me ):
  43. self.down = FALSE # done recording points
  44. self.update() # draw the lines
  45. #
  46. # Handles mouse move events for the connect widget.
  47. #
  48. def mouseMoveEvent(self, me):
  49. if self.down and self.count < MAXPOINTS:
  50. paint = TQPainter( self )
  51. self.points[self.count] = TQPoint(me.pos()) # add point
  52. paint.drawPoint( me.pos() ) # plot point
  53. self.count = self.count+1
  54. #
  55. # Create and display a ConnectWidget.
  56. #
  57. a = TQApplication( sys.argv )
  58. connect = ConnectWidget()
  59. connect.setCaption( "PyTQt Example - Draw lines")
  60. a.setMainWidget( connect )
  61. connect.show()
  62. a.exec_loop()