Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. import sys
  2.  
  3. from PyQt4.QtCore import *
  4.  
  5. from PyQt4.QtGui import *
  6.  
  7. from PyQt4 import QtGui,QtCore
  8.  
  9. import psycopg2
  10.  
  11.  
  12.  
  13. class MyWindow(QWidget):
  14.  
  15. def __init__(self):
  16.  
  17. QWidget.__init__(self)
  18.  
  19.  
  20.  
  21. # create table
  22.  
  23. self.get_table_data()
  24.  
  25. self.table = self.createTable()
  26.  
  27. # layout
  28.  
  29. self.layout = QVBoxLayout()
  30.  
  31. self.initClasses()
  32.  
  33. #self.testButton = QPushButton("test")
  34.  
  35. self.setWindowTitle("Inventaire")
  36.  
  37. #Main Size
  38.  
  39. screen = QtGui.QDesktopWidget().screenGeometry()
  40.  
  41. self.showMaximized()
  42.  
  43.  
  44.  
  45. #self.layout.addWidget(self.testButton)
  46.  
  47. self.layout.addWidget(self.groupBox1) # add widget
  48.  
  49. self.layout.addWidget(self.labelGroup1_ShotInfo) # add widget
  50.  
  51.  
  52.  
  53. # set my layout to make sure contents are correctly rendered
  54.  
  55. self.setLayout(self.layout)
  56.  
  57. self.layout.addWidget(self.table)
  58.  
  59. #self.setLayout(self.layout)
  60.  
  61. def initClasses(self):
  62.  
  63. # GroupBox
  64.  
  65. self.groupBox1 = GroupBox(self, QRect(20, 10, 180, 131), 'Shot Info')
  66.  
  67.  
  68.  
  69. # Label
  70.  
  71. self.labelGroup1_ShotInfo = Label(self, QRect(20, 40, 40, 15), 'Film')
  72.  
  73.  
  74.  
  75. def get_table_data(self):
  76.  
  77. conn = psycopg2.connect(database="sammy", user="postgres", password="a", host="localhost", port="5432")
  78.  
  79. cur = conn.cursor()
  80.  
  81. cur.execute("SELECT * from conn")
  82.  
  83. self.tabledata = cur.fetchall()
  84.  
  85.  
  86.  
  87. def createTable(self):
  88.  
  89. # create the view
  90.  
  91. tv = QTableView()
  92.  
  93.  
  94.  
  95. # set the table model
  96.  
  97. header = ['id', 'nom', 'password']
  98.  
  99. tablemodel = MyTableModel(self.tabledata, header, self)
  100.  
  101. tv.setModel(tablemodel)
  102.  
  103.  
  104.  
  105. # set the minimum size
  106.  
  107. tv.setMinimumSize(400, 300)
  108.  
  109.  
  110.  
  111. # hide grid
  112.  
  113. tv.setShowGrid(False)
  114.  
  115.  
  116.  
  117. # hide vertical header
  118.  
  119. vh = tv.verticalHeader()
  120.  
  121. vh.setVisible(False)
  122.  
  123.  
  124.  
  125. # set horizontal header properties
  126.  
  127. hh = tv.horizontalHeader()
  128.  
  129. hh.setStretchLastSection(True)
  130.  
  131.  
  132.  
  133. # set column width to fit contents
  134.  
  135. tv.resizeColumnsToContents()
  136.  
  137.  
  138.  
  139. # set row height
  140.  
  141. tv.resizeRowsToContents()
  142.  
  143.  
  144.  
  145. # enable sorting
  146.  
  147. tv.setSortingEnabled(False)
  148.  
  149.  
  150.  
  151. #postion de Qtable
  152.  
  153.  
  154.  
  155. tv.setMaximumSize(5,5)
  156.  
  157. #test
  158.  
  159. tv.setGeometry(50,50,50,50)
  160.  
  161.  
  162.  
  163. return tv
  164.  
  165.  
  166.  
  167. class GroupBox(QWidget):
  168.  
  169. def __init__(self, parent, geo, title):
  170.  
  171. QWidget.__init__(self, parent)
  172.  
  173. obj = QGroupBox(parent)
  174.  
  175. obj.setGeometry(geo)
  176.  
  177. obj.setTitle(title)
  178.  
  179. class Label(QWidget):
  180. def __init__(self, parent, geo, text):
  181. QWidget.__init__(self, parent)
  182. obj = QLabel(parent)
  183. obj.setGeometry(geo)
  184. obj.setText(text)
  185. class MyTableModel(QAbstractTableModel):
  186. def __init__(self, datain, headerdata, parent=None):
  187. """
  188. Args:
  189. datain: a list of listsn
  190. headerdata: a list of strings
  191. """
  192. QAbstractTableModel.__init__(self, parent)
  193. self.arraydata = datain
  194. self.headerdata = headerdata
  195. def rowCount(self, parent):
  196. return len(self.arraydata)
  197. def columnCount(self, parent):
  198. if len(self.arraydata) > 0:
  199. return len(self.arraydata[0])
  200. return 0
  201. def data(self, index, role):
  202. if not index.isValide):
  203. return QVariant()
  204. elif role != Qt.DisplayRole:
  205. return QVariant()
  206. return QVariant(self.arraydata[index.row()][index.column()])
  207. def setData(self, index, value, role):
  208. pass # not sure what to put here
  209. def headerData(self, col, orientation, role):
  210. if orientation == Qt.Horizontal and role == Qt.DisplayRole:
  211. return QVariant(self.headerdata[col])
  212. return QVariant()
  213. def sort(self, Ncol, order):
  214. """
  215. Sort table by given column number.
  216. self.emit(SIGNAL("layoutAboutToBeChanged()"))
  217. self.arraydata = sorted(self.arraydata, key=operator.itemgetter(Ncol))
  218. if order == Qt.DescendingOrder:
  219. self.arraydata.reverse()
  220. self.emit(SIGNAL("layoutChanged()"))
  221. if __name__ == "__main__":
  222. app = QApplication(sys.argv)
  223. w = MyWindow()
  224. w.show()
  225. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement