PyQt: How to adjust QTableView header-size/column-width
我试图按照此链接中所述设置标题大小或列宽。
问题是:
1 2 | self.produktKaufTb.setColumnWidth(1, 80) self.produktKaufTb.horizontalHeader().setResizeMode(0, QHeaderView.Stretch) |
returns"AttributeError: 'QHeaderView' object has no attribute'setResizeMode'"
其他两个选项
1 | .horizontalHeader().setSectionResizeMode(1) |
要么
1 | .horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) |
不允许用鼠标调整列的大小
如何在PyQt5中设置列宽?
经过一番混乱后,我想出了一个解决方案(可能不是最佳解决方案,但希望会有所帮助)。
步骤1:我必须继承
步骤2:然后,我将headerview的
在这个工作示例下面,我添加了一个水平拆分器,以便您可以尝试使用表调整大小功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | import sys from PyQt5 import QtWidgets from PyQt5.QtCore import QAbstractTableModel, Qt, QVariant 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.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 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 while keep total width constant''' # keep a copy of column widths ws=[] for c in range(self.count()): wii=self.sectionSize(c) ws.append(wii) if args[0]>0 or args[0]<self.count(): for ii in range(args[0],self.count()): if ii==args[0]: # resize present column self.resizeSection(ii,args[2]) elif ii==args[0]+1: # if present column expands, shrink the one to the right self.resizeSection(ii,ws[ii]-(args[2]-args[1])) else: # keep all others as they were self.resizeSection(ii,ws[ii]) def resizeEvent(self, event): """Resize table as a whole, need this to enable resizing""" super(QtWidgets.QHeaderView, self).resizeEvent(event) self.setSectionResizeMode(1,QtWidgets.QHeaderView.Stretch) for column in range(self.count()): self.setSectionResizeMode(column, QtWidgets.QHeaderView.Stretch) width = self.sectionSize(column) self.setSectionResizeMode(column, QtWidgets.QHeaderView.Interactive) self.resizeSection(column, width) return class MainFrame(QtWidgets.QWidget): def __init__(self): super(MainFrame,self).__init__() self.initUI() def initUI(self): self.doc_table=self.createTable() dummy_box=QtWidgets.QLineEdit() hlayout=QtWidgets.QHBoxLayout() h_split=QtWidgets.QSplitter(Qt.Horizontal) h_split.addWidget(self.doc_table) h_split.addWidget(dummy_box) hlayout.addWidget(h_split) self.setLayout(hlayout) self.show() def createTable(self): # create some dummy data 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) tv.setHorizontalHeader(hh) tv.setModel(tablemodel) tv.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) tv.setShowGrid(True) hh.setSectionsMovable(True) hh.setStretchLastSection(False) # this may be optional: #hh.setSectionResizeMode(QtWidgets.QHeaderView.Stretch) 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,800,600) self.show() if __name__=='__main__': app=QtWidgets.QApplication(sys.argv) mainwindow=MainWindow() sys.exit(app.exec_()) |