gpt4 book ai didi

python - PYQT:水平和垂直标题

转载 作者:行者123 更新时间:2023-12-01 03:24:38 25 4
gpt4 key购买 nike

有谁知道如何向 QTableView 添加垂直和水平标题?我已经为此工作了几个小时,但我似乎无法弄清楚。当前结果: enter image description here

但是,我正在尝试生成这个:(抱歉,我是在 Excel 中完成的 - 不过,我希望您明白)。 enter image description here

这是我的代码:

from PyQt4 import QtCore, QtGui, uic
import sys
try:
from PyQt4.QtCore import QString
except ImportError:
QString = str

SYSTEM=0

class inovaTableModel( QtCore.QAbstractTableModel ):
def __init__(self, data = [[]], headers=[], parent=None):
QtCore.QAbstractTableModel.__init__(self, parent )
self.__data = data
self.__headers = headers

def rowCount(self, parent):
return len(self.__data)

def columnCount(self, parent):
return len(self.__data[0])

def flags(self, index):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable

def data(self, index, role):

if role == QtCore.Qt.EditRole:
row = index.row( )
column = index.column( )
return self.__data[row][column]

if role == QtCore.Qt.ToolTipRole:
row = index.row( )
column = index.column( )
return self.__data[row][column]

if role == QtCore.Qt.DisplayRole:
row = index.row( )
column = index.column( )
return self.__data[row][column]


def setData(self, index, value, role = QtCore.Qt.EditRole):
if role ==QtCore.Qt.EditRole:

row = index.row()
column = index.column()
self.dataChanged.emit(index, index)
return self.__data[row][column]

def headerData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:

if orientation == QtCore.Qt.Horizontal:
return QString("X","Y","Z")
if orientation == QtCore.Qt.Vertical:
return QString("List ") + QString(section)

if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
app.setStyle( "plastique" )

data = ["one", "two", "three",]
tableData_alpha = [
["5","1"],
["10", "3"],
]


headers = ["Y", "X", "Z"]
model = inovaTableModel(tableData_alpha)

tableView = QtGui.QTableView()
tableView.show()
tableView.setModel(model)

sys.exit(app.exec_())

我将 headerData 添加到模型类中。我可以使垂直标题正常工作,但不能使水平标题正常工作。这是结果:

enter image description here

最佳答案

标题数据是每列/行的,因此您必须返回特定部分的标题,而不是全部 3 个部分。 str("X", "Y", "Z") str 的使用无效,您只需返回由 section 的值标识的值:

def headerData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return ["X", "Y", "Z"][section]
if orientation == QtCore.Qt.Vertical:
return QString("List ") + QString(section)

关于python - PYQT:水平和垂直标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41510927/

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