-
- import sys
- import operator
- from PyQt5 import QtWidgets, QtCore
- from PyQt5.QtCore import QAbstractTableModel, Qt, QVariant
- from PyQt5.QtGui import QPixmap, QIcon, QFont, QBrush, QColor
-
- def getXExpandYExpandSizePolicy():
- sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding,
- QtWidgets.QSizePolicy.Expanding)
- return sizePolicy
-
- class TableModel(QAbstractTableModel):
- def __init__(self, parent, datain, headerdata):
- QAbstractTableModel.__init__(self, parent)
-
- self.arraydata=datain
- self.headerdata=headerdata
-
- def rowCount(self,p):
- return len(self.arraydata)
-
- def columnCount(self,p):
- if len(self.arraydata)>0:
- return len(self.arraydata[0])
- return 0
-
- def data(self, index, role):
- if not index.isValid():
- return QVariant()
- elif role == Qt.BackgroundRole:
- if index.row()%2==0:
- return QBrush(QColor(220,220,240))
- elif role != Qt.DisplayRole:
- return QVariant()
- return QVariant(self.arraydata[index.row()][index.column()])
-
-
- def headerData(self, col, orientation, role):
- if orientation==Qt.Horizontal and role==Qt.DisplayRole:
- return self.headerdata[col]
- return None
-
- def sort(self,col,order):
- if col!=0:
- self.layoutAboutToBeChanged.emit()
- self.arraydata=sorted(self.arraydata,key=operator.itemgetter(col))
- if order==Qt.DescendingOrder:
- self.arraydata.reverse()
- self.layoutChanged.emit()
-
- class MyHeaderView(QtWidgets.QHeaderView):
- def __init__(self,parent):
- QtWidgets.QHeaderView.__init__(self,Qt.Horizontal,parent)
- self.sectionResized.connect(self.myresize)
-
- def myresize(self, *args):
- """ Resize all sections to content and user interactive """
-
- header=self
- #header.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
- #header.setSectionResizeMode(QtWidgets.QHeaderView.Interactive)
- total_width=0
- ws=[]
- for c in range(header.count()):
- wii=header.sectionSize(c)
- ws.append(wii)
- total_width+=wii
-
- if args[0]>0 or args[0]<header.count():
- for ii in range(args[0],header.count()):
- if ii==args[0]:
- header.resizeSection(ii,args[2])
- elif ii==args[0]+1:
- #this=ws[args[0]]
- nexto=ws[ii]
- new=nexto-(args[2]-args[1])
- header.resizeSection(ii,new)
- else:
- header.resizeSection(ii,ws[ii])
-
- def resizeEvent(self, event):
- """ Resize all sections to content and user interactive """
-
- super(QtWidgets.QHeaderView, self).resizeEvent(event)
- header=self
- header.setSectionResizeMode(1,QtWidgets.QHeaderView.Stretch)
- for column in range(header.count()):
- header.setSectionResizeMode(column, QtWidgets.QHeaderView.Stretch)
- width = header.sectionSize(column)
- print 'column',column,'width',width
- header.setSectionResizeMode(column, QtWidgets.QHeaderView.Interactive)
- header.resizeSection(column, width)
-
- width = header.sectionSize(column)
- print 'column',column,'width2',width
-
- header.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
- header.setSectionResizeMode(QtWidgets.QHeaderView.Interactive)
-
- class MainFrame(QtWidgets.QWidget):
-
- def __init__(self):
- super(MainFrame,self).__init__()
- self.initUI()
-
- def initUI(self):
-
- self.doc_table=self.createTable()
- hlayout=QtWidgets.QHBoxLayout()
- hlayout.addWidget(self.doc_table)
- self.setLayout(hlayout)
- self.show()
-
- def createTable(self):
- self.tabledata=[['aaa' ,' title1', True, 1999],
- ['bbb' ,' title2', True, 2000],
- ['ccc' ,' title3', False, 2001]
- ]
- header=['author', 'title', 'read', 'year']
- tablemodel=TableModel(self,self.tabledata,header)
-
- tv=QtWidgets.QTableView(self)
- #hh=MyHeaderView(self)
- hh=tv.horizontalHeader()
- #tv.setHorizontalHeader(hh)
- tv.setModel(tablemodel)
- #tv.setSizePolicy(getXExpandYExpandSizePolicy())
- tv.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
- tv.setShowGrid(True)
-
- hh.setSectionsMovable(True)
- hh.setStretchLastSection(False)
- hh.setSectionResizeMode(1,QtWidgets.QHeaderView.Stretch)
- print hh.stretchSectionCount()
- tv.setSortingEnabled(True)
-
- return tv
-
-
- class MainWindow(QtWidgets.QMainWindow):
-
- def __init__(self):
- super(MainWindow,self).__init__()
-
- self.main_frame=MainFrame()
- self.setCentralWidget(self.main_frame)
- self.setGeometry(100,100,1200,900) #(x_left,y_top,w,h)
- self.show()
-
-
- if __name__=='__main__':
-
- app=QtWidgets.QApplication(sys.argv)
- mainwindow=MainWindow()
- sys.exit(app.exec_())
-