2012-11-15 64 views
0

非常簡單的任務,但我沒有設法找到任何有用的文檔。我希望QTreeView包含一個名爲「Files」的列,其中包含來自QFileSystemView的數據。這裏是我得到的:QTreeView/QFileSystemModel設置標題標籤

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); 
    } 

這給了我一個帶有「名稱」標題的列。我如何重命名這個頭文件?

回答

3

QAbstractItemModel::setHeaderData()應該工作。如果不是,則可以始終從QFileSystemModel繼承並覆蓋headerData()

+3

我只是想相同的(使用'setHeaderData()'),但它沒有工作。查看源代碼'src/gui/dialogs/qfilesystemmodel.cpp' - 頭文件被硬編碼在那裏:(所以,對'QFileSystemModel'進行子類化並重載'headerData()'是正確的解決方案。 –

0

快速,但有點使壞(請注意: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(); 
} 
0

你也可以繼承QFileSystemModel和超越控制方法headerData()。例如,如果你想只改變第一首標,並留下其原始值的休息,你可以這樣做:

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); 
    } 
}