Alexandre
Alexandre

Reputation: 73

QTreeView change column name

I'm working with QTreeView and QFileSystemModel. How I can change a column name?

This is a sample of my code:

startDir = "/home/abusquets/cads"
filter = ["*.dxf"]

model = QtGui.QFileSystemModel()
model.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot | QDir.AllEntries)
model.setRootPath(startDir)

#Només volem fitxers dxf
model.setNameFilters(filter)
model.setNameFilterDisables(0)

tree = QtGui.QTreeView()
tree.setModel(model)
tree.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)

tree.setRootIndex(model.index(startDir)) 
self.setCentralWidget(tree)

Upvotes: 5

Views: 3879

Answers (2)

yurenchen
yurenchen

Reputation: 2483

in QStandardItemModel, you can do it:

model->setHeaderData(0,Qt::Horizontal, "---header0---");

but, in QFileSystemModel, the

headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const

was reimplemented.

methord 1

you need a new class inherited from QFileSystemModel, and reimplemente

headerData() again.

methord 2

use a delegate model

methord 3

set the header model independently.

QStandardItemModel *model1=new QStandardItemModel(0,5,this);

model1->setHeaderData(0, Qt::Horizontal, "header0");
model1->setHeaderData(1, Qt::Horizontal, "header1");

tree->header()->setModel(model1);

the last method was most simple.

Upvotes: 5

Alexandre
Alexandre

Reputation: 73

Thanks yurenchen.

I've solved like this:

class MyQFileSystemModel(QtGui.QFileSystemModel):

    def headerData(self, section, orientation, role):
        if section == 0 and role == Qt.DisplayRole:
            return "Nom"
        else:
            return super(QtGui.QFileSystemModel, self).headerData(section, orientation, role)

Upvotes: 2

Related Questions