擅长:python、mysql、java
<p>如果对自己的模型使用<code>QTableView</code>,则需要在模型中实现<a href="http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qabstractitemmodel.html#headerData">^{<cd2>}</a>方法以返回头的数据。这里有一个片段只显示列标题-更改<code>header_labels</code>值以更改标题文本。</p>
<pre><code>class TableModel(QAbstractTableModel):
header_labels = ['Column 1', 'Column 2', 'Column 3', 'Column 4']
def __init__(self, parent=None):
QAbstractTableModel.__init__(self, parent)
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return self.header_labels[section]
return QAbstractTableModel.headerData(self, section, orientation, role)
</code></pre>