1

Pretty simple task but I didn't manage to find anything useful in documentation. I want a QTreeView to contain a single column called "Files" with data from QFileSystemView. Here's what I've got:

    QFileSystemModel *projectFiles = new QFileSystemModel();
    projectFiles->setRootPath(QDir::currentPath());
    ui->filesTree->setModel(projectFiles);
    ui->filesTree->setRootIndex(projectFiles->index(QDir::currentPath()));

    // hide all but first column
    for (int i = 3; i > 0; --i)
    {
        ui->filesTree->hideColumn(i);
    }

That gives me a single column with "Name" header. How do I rename this header?

jaho
  • 4,852
  • 6
  • 40
  • 66

3 Answers3

3

QAbstractItemModel::setHeaderData() should work. If not, you can always inherit from QFileSystemModel and override headerData().

Stephen Chu
  • 12,724
  • 1
  • 35
  • 46
  • 3
    I just tried the same (using `setHeaderData()`), but it did not work. Looked up the source `src/gui/dialogs/qfilesystemmodel.cpp` - the headers are hardcoded there :( So, subclassing `QFileSystemModel` and overriding `headerData()` is the right solution. – Andreas Fester Nov 15 '12 at 21:04
0

Quick but a little dirty trick (please note w.hideColumn()):

#include <QApplication>

#include <QFileSystemModel>
#include <QTreeView>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTreeView w;

    QFileSystemModel m;
    m.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
    m.setRootPath("C:\\");

    w.setModel(&m);
    w.setRootIndex(m.index(m.rootPath()));
    w.hideColumn(3);
    w.hideColumn(2);
    w.hideColumn(1);

    w.show();

    return a.exec();
}
Kirill Gamazkov
  • 3,277
  • 1
  • 18
  • 22
0

You can subclass QFileSystemModel and overide method headerData(). For example, if you want only to change first header label and leave the rest with their original values, you can do:

QVariant MyFileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const {

    if ((section == 0) && (role == Qt::DisplayRole)) {
        return "Folder";
    } else {
        return QFileSystemModel::headerData(section,orientation,role);
    }
}
Roberto Mier
  • 59
  • 1
  • 1