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.

tut8.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python
  2. # TQt tutorial 8.
  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.setPalette(qt.TQPalette(qt.TQColor(250, 250, 200)))
  28. def angle(self):
  29. return self.ang
  30. def setAngle(self, degrees):
  31. if degrees < 5:
  32. degrees = 5
  33. if degrees > 70:
  34. degrees = 70
  35. if self.ang == degrees:
  36. return
  37. self.ang = degrees
  38. self.repaint()
  39. self.emit(qt.PYSIGNAL("angleChanged(int)"), (self.ang, ))
  40. def paintEvent(self, ev):
  41. p = qt.TQPainter(self)
  42. p.drawText(200, 200, "Angle = %d" % (self.ang))
  43. def sizePolicy(self):
  44. return qt.TQSizePolicy(qt.TQSizePolicy.Expanding, qt.TQSizePolicy.Expanding)
  45. class MyWidget(qt.TQWidget):
  46. def __init__(self, parent=None, name=None):
  47. qt.TQWidget.__init__(self, parent, name)
  48. quit = qt.TQPushButton("Quit", self, "quit")
  49. quit.setFont(qt.TQFont("Times", 18, qt.TQFont.Bold))
  50. self.connect(quit, qt.SIGNAL("clicked()"), qt.tqApp, qt.SLOT("quit()"))
  51. self.angle = LCDRange(self, "angle")
  52. self.angle.setRange(5, 70)
  53. self.cannonField = CannonField(self, "cannonField")
  54. self.connect(self.angle, qt.PYSIGNAL("valueChanged(int)"), self.cannonField.setAngle)
  55. self.connect(self.cannonField, qt.PYSIGNAL("angleChanged(int)"), self.angle.setValue)
  56. grid = qt.TQGridLayout(self, 2, 2, 10)
  57. grid.addWidget(quit, 0, 0)
  58. grid.addWidget(self.angle, 1, 0, qt.TQt.AlignTop)
  59. grid.addWidget(self.cannonField, 1, 1)
  60. grid.setColStretch(1, 10)
  61. self.angle.setValue(60)
  62. self.angle.setFocus()
  63. a = qt.TQApplication(sys.argv)
  64. w = MyWidget()
  65. w.setGeometry(100, 100, 500, 355)
  66. a.setMainWidget(w)
  67. w.show()
  68. sys.exit(a.exec_loop())