python - PYQT:水平和垂直标题

标签 python qt header pyqt qtableview

有谁知道如何向 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/

相关文章:

python - 如何从 Tensorflow 张量中获取第二个最大值?

c++ - 编辑 ui 文件时,不会重新生成 cpp.obj 文件

c++ - QtWebKit 如何处理登录提示

c++ - 用于 mingw 的 msi.h

c++ - 如何访问 tcp header 详细信息?

python - PySpark 从 s3 中的 zip 文件中读取两种不同文件类型的 csv

python - 为什么 python 不重新引用这个现有对象?

python - 为什么我的代码没有生成两个进程?

linux - 如何使用 GhostScript 避免标题截断 tiff。选择 于 tiff

c++ - 只需添加一些文档即可触发重新编译 : Is there a solution?