SIP4 python bindings for TQt
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

119 lines
3.9KB

  1. #!/usr/bin/env python
  2. #****************************************************************************
  3. #** $Id: sqltable4.py,v 1.3 2002/07/06 13:35:41 phil Exp $
  4. #**
  5. #** Copyright (C) 1992-1998 Troll Tech AS. All rights reserved.
  6. #**
  7. #** This file is part of an example program for PyTQt. 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 python_tqt.qtsql import *
  14. from dbconnect import createConnection
  15. TRUE = 1
  16. FALSE = 0
  17. class CustomTable(TQDataTable):
  18. def __init__(self, cursor, autoPopulate = FALSE, parent = None, name = None):
  19. TQDataTable.__init__(self, cursor, autoPopulate, parent, name)
  20. def paintField(self, p, field, cr, b):
  21. if not field:
  22. return
  23. fn = str(field.name())
  24. if fn in ("salary", "monsalary"):
  25. v = field.value().toDouble()
  26. if v < 0:
  27. p.setPen(TQColor("red"))
  28. value = TQString(u"%.2f \u20ac" % v)
  29. #print unicode(value).encode("iso-8859-15")
  30. p.drawText(2, 2, cr.width()-6, cr.height()-4,
  31. TQt.AlignRight|TQt.AlignVCenter, value)
  32. elif fn == "statusid":
  33. query = TQSqlQuery("SELECT name FROM status WHERE id=%s" %
  34. field.value().toString())
  35. value = ""
  36. if query.next():
  37. value = query.value(0).toString()
  38. p.drawText(2, 2, cr.width()-4, cr.height()-4,
  39. self.fieldAlignment(field), value)
  40. else:
  41. TQDataTable.paintField(self, p, field, cr, b)
  42. class StatusPicker(TQComboBox):
  43. def __init__(self, parent = None, name = None):
  44. TQComboBox.__init__(self, parent, name)
  45. cur = TQSqlCursor("status")
  46. cur.select(cur.index("id"))
  47. while cur.next():
  48. self.insertItem(cur.value("name").toString(), cur.value("id").toInt())
  49. class CustomSqlEditorFactory(TQSqlEditorFactory):
  50. def __init__(self):
  51. TQSqlEditorFactory.__init__(self)
  52. def createEditor(self, parent, field):
  53. try:
  54. if str(field.name()) == "statusid":
  55. return StatusPicker(parent)
  56. except AttributeError:
  57. pass
  58. return TQSqlEditorFactory.createEditor(self, parent, field)
  59. class StaffCursor(TQSqlCursor):
  60. def __init__(self):
  61. TQSqlCursor.__init__(self, "staff")
  62. monSalary = TQSqlFieldInfo("monsalary", TQVariant.Double)
  63. self.append(monSalary)
  64. self.setCalculated(monSalary.name(), TRUE)
  65. def calculateField(self, name):
  66. if str(name) == "monsalary":
  67. return TQVariant(self.value("salary").toDouble() / 12)
  68. return TQVariant(TQString.null)
  69. class Table(CustomTable):
  70. def __init__(self):
  71. #self.staffCursor = TQSqlCursor("staff")
  72. self.staffCursor = StaffCursor()
  73. TQDataTable.__init__(self, self.staffCursor)
  74. self.propMap = TQSqlPropertyMap()
  75. self.editorFactory = CustomSqlEditorFactory()
  76. self.propMap.insert("StatusPicker", "statusid")
  77. self.installPropertyMap(self.propMap)
  78. self.installEditorFactory(self.editorFactory)
  79. for cn, ch in (("forename", "Forename"),
  80. ("surname", "Surname" ),
  81. ("salary", "Annual Salary"),
  82. ("monsalary","Monthly Salary"),
  83. ("statusid", "Status")):
  84. self.addColumn(cn, ch)
  85. self.order = TQStringList("surname")
  86. self.order.append("forename")
  87. self.setSort(self.order)
  88. self.refresh()
  89. if __name__=='__main__':
  90. app = TQApplication(sys.argv)
  91. #app.setFont(TQFont("Verdana", 11))
  92. if createConnection():
  93. t = Table()
  94. app.setMainWidget(t)
  95. t.resize(600, 250)
  96. t.show()
  97. app.exec_loop()