【问题标题】:about the QT class QFileSystemModel关于 QT 类 QFileSystemModel
【发布时间】:2015-06-08 12:14:57
【问题描述】:

我想使用 QFileSystemModel 类在 QTreeView 中显示我的文件系统。 但是当我在其中设置 thr 根路径时。我尝试获取它的第一个文件名。它失败了。我想问我应该如何获取父母的孩子信息。谢谢

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTreeView view;
    QFileSystemModel *model = new QFileSystemModel(&view);
    model->setRootPath(QDir::currentPath());
    view.setModel(model);
    view.setCurrentIndex(model->index(model->rootPath()));
    QModelIndex index = model->index(model->rootPath());
    qDebug() << model->fileName(index);   
    qDebug() << model->fileName(index.child(0,0));
    view.show();
    return a.exec();
}

【问题讨论】:

  • 查看bogotobogo.com/Qt/… 的示例。你不需要QModelIndex
  • 感谢您的评论。上面的示例在节点单击时可以正常运行。但是,如果我希望它在加载被点击时运行。我可以得到所有rootpath的子目录,因为我已经设置了rootpath。现在我想在树加载时获得访问它的子文件和子目录的方式

标签: qt


【解决方案1】:

尝试:

QFileSystemModel *model = new QFileSystemModel;
model->setRootPath(QDir::currentPath());
QTreeView *tree = new QTreeView(splitter);
tree->setModel(model);

splitter是父对话框,视图将驻留在其中。

【讨论】:

  • 感谢您的评论。上面的示例在节点单击时可以正常运行。但是,如果我希望它在加载被点击时运行。因为我已经设置了根路径,所以我可以获得所有根路径的子目录。现在我想在树加载时获得访问其子文件和子目录的方式。
  • 不谢,如果您在我的回答/评论中发现有用的内容,请点赞。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
相关资源
最近更新 更多