Guest User

Untitled

a guest
Feb 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. import operator # used for sorting
  2. from PyQt4.QtCore import *
  3. from PyQt4.QtGui import *
  4. from PyQt4 import QtGui, QtCore
  5.  
  6. class MyTable(QWidget):
  7. Checkboxcheckvalue = pyqtSignal(bool)
  8. Rowselected = pyqtSignal(int)
  9. doubleClicked = pyqtSignal(int)
  10. def __init__(self, *args):
  11. QWidget.__init__(self, *args)
  12. # setGeometry(x_pos, y_pos, width, height)
  13. # self.setGeometry(70, 150, 1326, 582)
  14. self.dataList = []
  15. self.header = []
  16. self.currentrow = []
  17. self.setWindowTitle("Click on the header to sort table")
  18.  
  19. self.table_model = MyTableModel(self, self.dataList, self.header)
  20. self.table_view = QTableView()
  21. #self.table_view.setSelectionMode(QAbstractItemView.SingleSelection)
  22. self.table_view.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
  23. self.table_view.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
  24. # bind cell click to a method reference
  25. self.table_view.clicked.connect(self.showSelection)
  26. self.table_view.clicked.connect(self.selectRow)
  27.  
  28. self.table_view.doubleClicked.connect(self.doubleclickedaction)
  29.  
  30. self.table_model.CheckBoxValue.connect(self.checkboxchecked)
  31.  
  32.  
  33.  
  34.  
  35. self.table_view.setModel(self.table_model)
  36. # enable sorting
  37. self.table_view.setSortingEnabled(True)
  38.  
  39. layout = QVBoxLayout(self)
  40. layout.addWidget(self.table_view)
  41. self.setLayout(layout)
  42.  
  43.  
  44.  
  45. def update_model(self):
  46. self.table_model = MyTableModel(self, self.dataList, self.header)
  47. self.table_view.setModel(self.table_model)
  48. self.table_view.update()
  49.  
  50. def findLabelName(self,rowindex):
  51. return self.dataList[rowindex][-1]
  52.  
  53. def doubleclickedaction(self,index):
  54. self.doubleClicked.emit(index.row())
  55. self.currentrow = index.row()
  56. print ('doubleclicked',self.findLabelName(index.row()))
  57.  
  58.  
  59. def checkboxchecked(self,value):
  60. print ('table checkboxchecked',self.currentrow,value)
  61. # self.currentrow = index.row()
  62. if value == True:
  63. self.Checkboxcheckvalue.emit(True)
  64. else:
  65. self.Checkboxcheckvalue.emit(False)
  66.  
  67.  
  68. def selectedLabel(self,index):
  69. return self.findLabelName(index.row())
  70.  
  71.  
  72.  
  73. def showSelection(self, item):
  74. cellContent = item.data()
  75. # print(cellContent) # test
  76. sf = "You clicked on {}".format(cellContent)
  77. # display in title bar for convenience
  78. self.setWindowTitle(sf)
  79.  
  80. def selectRow(self, index):
  81. self.Rowselected.emit(index.row())
  82. self.currentrow = index.row()
  83. print("current row is %d", index.row())
  84. pass
  85.  
  86.  
  87. class MyTableModel(QAbstractTableModel):
  88. """
  89. keep the method names
  90. they are an integral part of the model
  91. """
  92.  
  93. CheckBoxValue = pyqtSignal(bool)
  94. def __init__(self, parent, mylist, header, *args):
  95. QAbstractTableModel.__init__(self, parent, *args)
  96. self.mylist = mylist
  97. self.header = header
  98.  
  99. def setDataList(self, mylist):
  100. self.mylist = mylist
  101. self.layoutAboutToBeChanged.emit()
  102. self.dataChanged.emit(self.createIndex(0, 0), self.createIndex(self.rowCount(0), self.columnCount(0)))
  103. self.layoutChanged.emit()
  104.  
  105. def rowCount(self, parent):
  106. if self.mylist != []:
  107. return len(self.mylist)
  108. else:
  109. return 0
  110.  
  111. def columnCount(self, parent):
  112. if self.mylist != []:
  113. return len(self.mylist[0])-1
  114. else:
  115. return 0
  116.  
  117. def data(self, index, role):
  118. if not index.isValid():
  119. return None
  120. if (index.column() == 0):
  121. value = self.mylist[index.row()][index.column()].text()
  122. else:
  123. value = self.mylist[index.row()][index.column()]
  124. if role == QtCore.Qt.EditRole:
  125. return value
  126. elif role == QtCore.Qt.DisplayRole:
  127. return value
  128. elif role == QtCore.Qt.CheckStateRole:
  129. if index.column() == 0:
  130. if self.mylist[index.row()][index.column()].isChecked():
  131. return QtCore.Qt.Checked
  132. else:
  133. return QtCore.Qt.Unchecked
  134.  
  135. def headerData(self, col, orientation, role):
  136. if orientation == Qt.Horizontal and role == Qt.DisplayRole:
  137. return self.header[col]
  138. return None
  139.  
  140. def sort(self, col, order):
  141. """sort table by given column number col"""
  142. if col != 0:
  143. pass
  144. else:
  145. col = -1
  146. self.emit(SIGNAL("layoutAboutToBeChanged()"))
  147. self.mylist = sorted(self.mylist, key=operator.itemgetter(col))
  148. if order == Qt.DescendingOrder:
  149. self.mylist.reverse()
  150. self.emit(SIGNAL("layoutChanged()"))
  151. print 'sorted'
  152.  
  153. def flags(self, index):
  154. if not index.isValid():
  155. return None
  156. if index.column() == 0:
  157. return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsUserCheckable
  158. else:
  159. return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
  160.  
  161. def setData(self, index, value, role):
  162. if not index.isValid():
  163. return False
  164. if role == QtCore.Qt.CheckStateRole and index.column() == 0:
  165. if value == QtCore.Qt.Checked:
  166. self.mylist[index.row()][index.column()].setChecked(True)
  167. print('checked',index.row())
  168. self.CheckBoxValue.emit(True)
  169.  
  170. else:
  171. self.mylist[index.row()][index.column()].setChecked(False)
  172. print('unchecked',index.row())
  173. self.CheckBoxValue.emit(False)
  174. self.dataChanged.emit(index, index)
  175. return True
  176.  
  177. if __name__ == '__main__':
  178. app = QApplication([])
  179. header = ['name', 'type', 'segment', 'exit', 'entry']
  180. dataList = [
  181. [QtGui.QCheckBox('line9'), 'line', '058176', '01', '1705','line9'],
  182. [QtGui.QCheckBox('line3'), 'line', '058176', '02', '1705','line3'],
  183. [QtGui.QCheckBox('line6'), 'line', '058176', '03', '1705','line6'],
  184. [QtGui.QCheckBox('line1'), 'line', '058176', '04', '1705','line'],
  185. [QtGui.QCheckBox('line4'), 'line', '058176', '01', '1705','line4'],
  186. [QtGui.QCheckBox('area4'), 'area', '058176', '02', '1705','area4'],
  187. [QtGui.QCheckBox('area2'), 'area', '058176', '02', '1705','area2'],
  188. [QtGui.QCheckBox('area8'), 'area', '058176', '01', '1705','area8'],
  189. ]
  190.  
  191. win = MyTable()
  192. win.dataList = dataList
  193. win.header = header
  194. win.update_model()
  195. win.show()
  196. app.exec_()
Add Comment
Please, Sign In to add comment