gpt4 book ai didi

python - 如何拉伸(stretch) QTableView 最后一列标题

转载 作者:行者123 更新时间:2023-11-28 22:47:37 24 4
gpt4 key购买 nike

下面的代码创建了只有一列的QTableView。如何使标题列沿 QTableView View 的整个宽度拉伸(stretch)?

enter image description here

from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)

class TableModel(QtCore.QAbstractTableModel):
def __init__(self):
QtCore.QAbstractTableModel.__init__(self)
def rowCount(self, parent=QtCore.QModelIndex()):
return 0
def columnCount(self, index=QtCore.QModelIndex()):
return 1
def headerData(self, column, orientation, role=QtCore.Qt.DisplayRole):
if role!=QtCore.Qt.DisplayRole: return QtCore.QVariant()
if orientation==QtCore.Qt.Horizontal: return QtCore.QVariant('Column Name')

class TableView(QtGui.QTableView):
def __init__(self):
super(TableView, self).__init__()
model=TableModel()
self.setModel(model)
self.show()

view=TableView()
sys.exit(app.exec_())

最佳答案

您正在寻找的是 QHeaderView::setResizeMode 函数。我建议查看 docs , 但这是代码

self.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)

或者,如果您只想拉伸(stretch)最少的标题项:

self.horizontalHeader().setStretchLastSection(True)

关于python - 如何拉伸(stretch) QTableView 最后一列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26070340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com