2014-12-19 69 views
1
下面

例子展示了如何“我的列名”标题名称是从TableView定义的范围内使用为中心,以控制QTableView中的标题:如何使用QAbstractTableModel

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

enter image description here

虽然这工作我想知道如何使用其headerData()方法从QAbstractTableModel内部控制Header

由于是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_()) 

回答

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

相关问题