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.

tut10.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python
  2. # TQt tutorial 10.
  3. import sys
  4. from python_tqt import qt
  5. class LCDRange(qt.TQVBox):
  6. def __init__(self, parent=None, name=None):
  7. qt.TQVBox.__init__(self, parent, name)
  8. lcd = qt.TQLCDNumber(2, self, "lcd")
  9. self.slider = qt.TQSlider(qt.TQt.Horizontal, self, "slider")
  10. self.slider.setRange(0, 99)
  11. self.slider.setValue(0)
  12. self.connect(self.slider, qt.SIGNAL("valueChanged(int)"), lcd, qt.SLOT("display(int)"))
  13. self.connect(self.slider, qt.SIGNAL("valueChanged(int)"), self, qt.PYSIGNAL("valueChanged(int)"))
  14. self.setFocusProxy(self.slider)
  15. def value(self):
  16. return self.slider.value()
  17. def setValue(self, value):
  18. self.slider.setValue(value)
  19. def setRange(self, minVal, maxVal):
  20. if minVal < 0 or maxVal > 99 or minVal > maxVal:
  21. raise ValueError, "LCDRange.setRange(): invalid range"
  22. self.slider.setRange(minVal, maxVal)
  23. class CannonField(qt.TQWidget):
  24. def __init__(self, parent=None, name=None):
  25. qt.TQWidget.__init__(self, parent, name)
  26. self.ang = 45
  27. self.f = 0
  28. self.setPalette(qt.TQPalette(qt.TQColor(250, 250, 200)))
  29. def angle(self):
  30. return self.ang
  31. def setAngle(self, degrees):
  32. if degrees < 5:
  33. degrees = 5
  34. if degrees > 70:
  35. degrees = 70
  36. if self.ang == degrees:
  37. return
  38. self.ang = degrees
  39. self.repaint(self.cannonRect(), 0)
  40. self.emit(qt.PYSIGNAL("angleChanged(int)"), (self.ang, ))
  41. def force(self):
  42. return self.f
  43. def setForce(self, newton):
  44. if newton < 0:
  45. newton = 0
  46. if self.f == newton:
  47. return
  48. self.f = newton
  49. self.emit(qt.PYSIGNAL("forceChanged(int)"), (self.f, ))
  50. def paintEvent(self, ev):
  51. if not ev.rect().intersects(self.cannonRect()):
  52. return
  53. cr = self.cannonRect()
  54. pix = qt.TQPixmap(cr.size())
  55. pix.fill(self, cr.topLeft())
  56. p = qt.TQPainter(pix)
  57. p.setBrush(qt.TQt.blue)
  58. p.setPen(qt.TQt.NoPen)
  59. p.translate(0, pix.height() - 1)
  60. p.drawPie(qt.TQRect(-35, -35, 70, 70), 0, 90 * 16)
  61. p.rotate(-self.ang)
  62. p.drawRect(qt.TQRect(33, -4, 15, 8))
  63. p.end()
  64. p.begin(self)
  65. p.drawPixmap(cr.topLeft(), pix)
  66. def cannonRect(self):
  67. r = qt.TQRect(0, 0, 50, 50)
  68. r.moveBottomLeft(self.rect().bottomLeft())
  69. return r
  70. def sizePolicy(self):
  71. return qt.TQSizePolicy(qt.TQSizePolicy.Expanding, qt.TQSizePolicy.Expanding)
  72. class MyWidget(qt.TQWidget):
  73. def __init__(self, parent=None, name=None):
  74. qt.TQWidget.__init__(self, parent, name)
  75. quit = qt.TQPushButton("&Quit", self, "quit")
  76. quit.setFont(qt.TQFont("Times", 18, qt.TQFont.Bold))
  77. self.connect(quit, qt.SIGNAL("clicked()"), qt.tqApp, qt.SLOT("quit()"))
  78. self.angle = LCDRange(self, "angle")
  79. self.angle.setRange(5, 70)
  80. self.force = LCDRange(self, "force")
  81. self.force.setRange(10, 50)
  82. self.cannonField = CannonField(self, "cannonField")
  83. self.connect(self.angle, qt.PYSIGNAL("valueChanged(int)"), self.cannonField.setAngle)
  84. self.connect(self.cannonField, qt.PYSIGNAL("angleChanged(int)"), self.angle.setValue)
  85. self.connect(self.force, qt.PYSIGNAL("valueChanged(int)"), self.cannonField.setForce)
  86. self.connect(self.cannonField, qt.PYSIGNAL("forceChanged(int)"), self.force.setValue)
  87. grid = qt.TQGridLayout(self, 2, 2, 10)
  88. grid.addWidget(quit, 0, 0)
  89. grid.addWidget(self.cannonField, 1, 1)
  90. grid.setColStretch(1, 10)
  91. leftBox = qt.TQVBoxLayout()
  92. grid.addLayout(leftBox, 1, 0)
  93. leftBox.addWidget(self.angle)
  94. leftBox.addWidget(self.force)
  95. self.angle.setValue(60)
  96. self.force.setValue(25)
  97. self.angle.setFocus()
  98. qt.TQApplication.setColorSpec(qt.TQApplication.CustomColor)
  99. a = qt.TQApplication(sys.argv)
  100. w = MyWidget()
  101. w.setGeometry(100, 100, 500, 355)
  102. a.setMainWidget(w)
  103. w.show()
  104. sys.exit(a.exec_loop())