如何让QTableView在初始时显示标题标签

时间:2017-10-13 07:33:33

标签: python qtableview

我使用QTableView和QAbstractTableModel来显示我的数据,

当我启动我的MainFrame时,QTableView中没有任何内容,因为我不知道 将数据设置为TableModel,

但是我想要显示Header,我该怎么做?

有示例代码

class MainFrame(QMainFrame):
    ....do something...
    self.tableView = QtWidgets.QTableView(self.centralWidget)
    self.tableView.setGeometry(QtCore.QRect(10, 10, 531, 161))
    self.tableView.setObjectName("tableView")
    self.myModel = MyModel(self)
    # self.myModel.update([[1, 2], [2, 3]])
    self.tableView.setModel(self.myModel)
    ....do something....

和MyModel Class,

class MyModel(QAbstractTableModel):
    header_labels = ['Column1','Column2','Column3','Column4','Column5']
    def __init__(self, parent=None, *args):
        super(MyModel, self).__init__()
        self.datatable = None

    def update(self, dataIn):
        .....
    def rowCount(self, parent=QtCore.QModelIndex()):
        .....
    def columnCount(self, parent=QtCore.QModelIndex()):
        .....
    def data(self, index, role=QtCore.Qt.DisplayRole):
        .....
    def flags(self, index):
        .....
    def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
        if role == QtCore.Qt.DisplayRole and orientation == QtCore.Qt.Horizontal:
            return self.header_labels[section]
        return QAbstractTableModel.headerData(self, section, orientation, role)

0 个答案:

没有答案