spacepaste

  1.  
  2. import sys
  3. import operator
  4. from PyQt5 import QtWidgets, QtCore
  5. from PyQt5.QtCore import QAbstractTableModel, Qt, QVariant
  6. from PyQt5.QtGui import QPixmap, QIcon, QFont, QBrush, QColor
  7. def getXExpandYExpandSizePolicy():
  8. sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding,
  9. QtWidgets.QSizePolicy.Expanding)
  10. return sizePolicy
  11. class TableModel(QAbstractTableModel):
  12. def __init__(self, parent, datain, headerdata):
  13. QAbstractTableModel.__init__(self, parent)
  14. self.arraydata=datain
  15. self.headerdata=headerdata
  16. def rowCount(self,p):
  17. return len(self.arraydata)
  18. def columnCount(self,p):
  19. if len(self.arraydata)>0:
  20. return len(self.arraydata[0])
  21. return 0
  22. def data(self, index, role):
  23. if not index.isValid():
  24. return QVariant()
  25. elif role == Qt.BackgroundRole:
  26. if index.row()%2==0:
  27. return QBrush(QColor(220,220,240))
  28. elif role != Qt.DisplayRole:
  29. return QVariant()
  30. return QVariant(self.arraydata[index.row()][index.column()])
  31. def headerData(self, col, orientation, role):
  32. if orientation==Qt.Horizontal and role==Qt.DisplayRole:
  33. return self.headerdata[col]
  34. return None
  35. def sort(self,col,order):
  36. if col!=0:
  37. self.layoutAboutToBeChanged.emit()
  38. self.arraydata=sorted(self.arraydata,key=operator.itemgetter(col))
  39. if order==Qt.DescendingOrder:
  40. self.arraydata.reverse()
  41. self.layoutChanged.emit()
  42. class MyHeaderView(QtWidgets.QHeaderView):
  43. def __init__(self,parent):
  44. QtWidgets.QHeaderView.__init__(self,Qt.Horizontal,parent)
  45. self.sectionResized.connect(self.myresize)
  46. def myresize(self, *args):
  47. """ Resize all sections to content and user interactive """
  48. header=self
  49. #header.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
  50. #header.setSectionResizeMode(QtWidgets.QHeaderView.Interactive)
  51. total_width=0
  52. ws=[]
  53. for c in range(header.count()):
  54. wii=header.sectionSize(c)
  55. ws.append(wii)
  56. total_width+=wii
  57. if args[0]>0 or args[0]<header.count():
  58. for ii in range(args[0],header.count()):
  59. if ii==args[0]:
  60. header.resizeSection(ii,args[2])
  61. elif ii==args[0]+1:
  62. #this=ws[args[0]]
  63. nexto=ws[ii]
  64. new=nexto-(args[2]-args[1])
  65. header.resizeSection(ii,new)
  66. else:
  67. header.resizeSection(ii,ws[ii])
  68. def resizeEvent(self, event):
  69. """ Resize all sections to content and user interactive """
  70. super(QtWidgets.QHeaderView, self).resizeEvent(event)
  71. header=self
  72. header.setSectionResizeMode(1,QtWidgets.QHeaderView.Stretch)
  73. for column in range(header.count()):
  74. header.setSectionResizeMode(column, QtWidgets.QHeaderView.Stretch)
  75. width = header.sectionSize(column)
  76. print 'column',column,'width',width
  77. header.setSectionResizeMode(column, QtWidgets.QHeaderView.Interactive)
  78. header.resizeSection(column, width)
  79. width = header.sectionSize(column)
  80. print 'column',column,'width2',width
  81. header.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
  82. header.setSectionResizeMode(QtWidgets.QHeaderView.Interactive)
  83. class MainFrame(QtWidgets.QWidget):
  84. def __init__(self):
  85. super(MainFrame,self).__init__()
  86. self.initUI()
  87. def initUI(self):
  88. self.doc_table=self.createTable()
  89. hlayout=QtWidgets.QHBoxLayout()
  90. hlayout.addWidget(self.doc_table)
  91. self.setLayout(hlayout)
  92. self.show()
  93. def createTable(self):
  94. self.tabledata=[['aaa' ,' title1', True, 1999],
  95. ['bbb' ,' title2', True, 2000],
  96. ['ccc' ,' title3', False, 2001]
  97. ]
  98. header=['author', 'title', 'read', 'year']
  99. tablemodel=TableModel(self,self.tabledata,header)
  100. tv=QtWidgets.QTableView(self)
  101. #hh=MyHeaderView(self)
  102. hh=tv.horizontalHeader()
  103. #tv.setHorizontalHeader(hh)
  104. tv.setModel(tablemodel)
  105. #tv.setSizePolicy(getXExpandYExpandSizePolicy())
  106. tv.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
  107. tv.setShowGrid(True)
  108. hh.setSectionsMovable(True)
  109. hh.setStretchLastSection(False)
  110. hh.setSectionResizeMode(1,QtWidgets.QHeaderView.Stretch)
  111. print hh.stretchSectionCount()
  112. tv.setSortingEnabled(True)
  113. return tv
  114. class MainWindow(QtWidgets.QMainWindow):
  115. def __init__(self):
  116. super(MainWindow,self).__init__()
  117. self.main_frame=MainFrame()
  118. self.setCentralWidget(self.main_frame)
  119. self.setGeometry(100,100,1200,900) #(x_left,y_top,w,h)
  120. self.show()
  121. if __name__=='__main__':
  122. app=QtWidgets.QApplication(sys.argv)
  123. mainwindow=MainWindow()
  124. sys.exit(app.exec_())
  125.