【问题标题】:PYQT: Horizontal and Vertical HeadersPYQT:水平和垂直标题
【发布时间】:2017-05-21 12:35:36
【问题描述】:

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

但是,我正在尝试制作这个:(抱歉,我是在 excel 中完成的——但是,我希望你能理解)。

这是我的代码:

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 添加到模型类。我能够让垂直标题工作,但不能让水平标题工作。结果如下:

【问题讨论】:

    标签: python qt header pyqt qtableview


    【解决方案1】:

    标题数据是每列/行的,因此您必须返回特定部分的标题,而不是全部 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)
    

    【讨论】:

    • 感谢您的回复。我正在使用模型视图编程。另外,我的代码发布在原始帖子中。我尝试在模型类上使用 setHeaderData;但是,它不起作用。
    • 我编辑了我的原始代码。我能够让垂直标题工作,但水平标题仍然有问题。
    • 啊,是的,答案已更新。 setHeaderData 支持用户编辑标题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 2013-12-10
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    相关资源
    最近更新 更多