这是一项非常简单的任务,但我没能在文档中找到任何有用的东西。我希望QTreeView包含一个名为"Files“的列,其中包含来自QFileSystemView的数据。下面是我得到的信息:
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);
}这给了我一个带有"Name“标题的列。如何重命名此标头?
发布于 2012-11-16 04:45:41
QAbstractItemModel::setHeaderData()应该可以工作。如果不是这样,您可以始终从QFileSystemModel继承并覆盖headerData()。
发布于 2013-01-27 17:11:58
快速但有点肮脏的把戏(请注意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();
}发布于 2013-08-28 07:00:48
您可以子类化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);
}
}https://stackoverflow.com/questions/13405728
复制相似问题