Reputation: 19329
The code below creates a simple QTableView
with a header and three QStandardItem
s.
Question: How to make its header to have three columns labeled as "Column 0", 'Column 1" and "Column 3"?
import os,sys
from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)
class Window(QtGui.QTableView):
def __init__(self):
super(Window, self).__init__()
header=QtGui.QHeaderView(QtCore.Qt.Horizontal, self)
self.setHorizontalHeader(header)
model=QtGui.QStandardItemModel()
for i in range(3):
model.appendRow(QtGui.QStandardItem('Item %s'%i))
self.setModel(model)
self.show()
window=Window()
sys.exit(app.exec_())
Upvotes: 2
Views: 4970
Reputation: 18504
You can also use this:
bool QAbstractItemModel.setHeaderData (self, int section, Qt.Orientation orientation, QVariant value, int role = Qt.EditRole)
But it requires set the sections and orientation.
Advantage:
You can set role, for example Qt::DecorationRole
, it allows you set images to your headers. You can combine different roles and get beautiuful results. For example, you can add simple text to image with Qt::DisplayRole
or Qt::EditRole
. As you can see, it is more flexible approach.
Upvotes: 1
Reputation: 28673
Use
model = QtGui.QStandardItemModel(0,3) # three columns
model.setHorizontalHeaderLabels( [ "Column 0", "Column 1", "Column 3"] )
Upvotes: 3