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]