如何解决删除QTableView最后一行下面的灰色区域而不进行单元格拉伸吗?
在相关solution处给出的question会删除给定图像中的灰带显示。如果您按照以下脚本中的操作将QtWidgets.QHeaderView.Stretch
更改为QtWidgets.QHeaderView.Fixed
,则该灰色带将返回,并且单元将以垂直或水平方式固定。
所需的是在不调整单元格高度或宽度大小的情况下删除灰色带。
代码:
import sys
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtCore import Qt
class TableModel(QtCore.QAbstractTableModel):
def __init__(self,data):
super(TableModel,self).__init__()
self._data = data
def data(self,index,role):
if role == Qt.displayRole:
return self._data[index.row()][index.column()]
def rowCount(self,index):
return len(self._data)
def columnCount(self,index):
return len(self._data[0])
def headerData(self,section,orientation,role):
# section is the index of the column/row.
if role == Qt.displayRole:
if orientation == Qt.Horizontal:
column_label = ['A','B1','B2']
return column_label[section]
if orientation == Qt.Vertical:
row_label = ['Device','ID','Operation','Weigth','Row 5','No Show']
return row_label[section]
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.table = QtWidgets.QTableView()
data = [
[4,9,2],[1,0],[3,5,3,[7,8,9],]
self.model = TableModel(data)
# self.table.verticalHeader().setDefaultSectionSize(50) # does not sove it.
# self.table.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
self.table.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
# self.table.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
self.table.setModel(self.model)
self.setCentralWidget(self.table)
app=QtWidgets.QApplication(sys.argv)
window=MainWindow()
window.show()
app.exec_()
注释2:here可以找到Martin Fitzpatrick的原始代码,并在此处进行了修改以显示问题。
解决方法
解决方案是创建一个QHeaderView,在其中绘制没有内容的部分:
class VerticalHeaderView(QtWidgets.QHeaderView):
def __init__(self,parent=None):
super().__init__(QtCore.Qt.Vertical,parent)
def paintEvent(self,event):
super().paintEvent(event)
h = self.offset()
for i in range(self.count()):
h += self.sectionSize(i)
if h < self.rect().bottom():
r = QtCore.QRect(self.rect())
r.moveTop(h)
painter = QtGui.QPainter(self.viewport())
painter.fillRect(r,QtGui.QColor("white"))
header = VerticalHeaderView(self.table)
self.table.setVerticalHeader(header)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。