如何使用QAbstractTableModel控制QTableView的标头

时间:2014-12-19 01:58:42

标签: python qt pyqt

下面的示例显示了“我的列名称”标题名称如何使用以下内容从TableView定义范围内居中:

self.horizontalHeader().setDefaultAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter) 

enter image description here

虽然这有效,但我想知道如何使用Header方法从QAbstractTableModel内部控制headerData()

headerData()返回标题名称:

if orientation==QtCore.Qt.Horizontal:
            return QtCore.QVariant('My Column Name')

如果是角色,它还会返回一个虚拟的QtCore.QVariant()!= QtCore.Qt.DisplayRole

    if role!=QtCore.Qt.DisplayRole:
        return QtCore.QVariant()

还有哪些其他角色和值可用于模型的headerData()

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

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)      

        self.items=['One','Two','Three','Four','Five','Six','Seven']

    def rowCount(self, parent=QtCore.QModelIndex()):   
        return len(self.items)
    def columnCount(self, index=QtCore.QModelIndex()):
        return 1

    def data(self, index, role):
        if not index.isValid() or not (0<=index.row()<len(self.items)):
            return QtCore.QVariant()

        item=str(self.items[index.row()])

        if role==QtCore.Qt.UserRole:
            return item

        if role==QtCore.Qt.DisplayRole:
            return item

        if role==QtCore.Qt.TextColorRole:
            return QtCore.QVariant(QtGui.QColor(QtCore.Qt.white))

        if role == QtCore.Qt.BackgroundRole:
            if index.row()%2:
                return QtCore.QVariant(QtGui.QColor("#242424"))
            else:
                return QtCore.QVariant(QtGui.QColor(QtCore.Qt.black))

    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('My Column Name') 

class TableView(QtGui.QTableView):
    def __init__(self, parent=None):
        super(TableView, self).__init__(parent)

        self.setBackgroundRole(QtGui.QPalette.Base)
        p=self.palette()
        p.setColor(self.backgroundRole(), QtGui.QColor((QtCore.Qt.black)))
        self.setPalette(p)

        self.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
        self.horizontalHeader().setDefaultAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)    

        font=QtGui.QFont()            
        font.setPointSize(9)        
        self.horizontalHeader().setFont(font)

        myModel=TableModel()
        self.setModel(myModel)      


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

1 个答案:

答案 0 :(得分:4)

表格视图的标题由QHeaderView提供。 The doc描述了它支持的数据角色:

  

QHeaderView尊重以下项目数据角色:TextAlignmentRole,   DisplayRole,FontRole,DecorationRole,ForegroundRole和   BackgroundRole。

以下是headerData实现的示例(代码在C ++中):

QVariant
Model::headerData(int section, Qt::Orientation orientation, int role) const
{
    ...
    if (role == Qt::DisplayRole)
    {
        return QString("Header #%1").arg(section);
    }

    if (role == Qt::FontRole)
    {
        QFont serifFont("Times", 10, QFont::Bold, true);
        return serifFont;
    }

    if (role == Qt::TextAlignmentRole)
    {
        return Qt::AlignRight;
    }

    if (role == Qt::BackgroundRole)
    {
        return QBrush(Qt::blue);
    }

    if (role == Qt::ForegroundRole)
    {
        return QBrush(Qt::red);
    }
    ...
}

还必须注意,BackgroundRole最有可能被窗口小部件选项板和常规应用程序样式覆盖。您可以查看this answer