QFileSystemModel setRootPath [英] QFileSystemModel setRootPath

查看:509
本文介绍了QFileSystemModel setRootPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Qt应用程序,该应用程序显示一个文件夹的内容(在Mac OS中为"Users"文件夹). 这是代码:

I am attempting to create a Qt application which shows the contents of a folder ("Users" folder in Mac OS). Here is the code:

QFileSystemModel *dirModel = new QFileSystemModel;
dirModel->setRootPath("/Users");

ui->listView->setModel(dirModel);

我还尝试使用此代码

当我运行该应用程序时,它没有显示"/Users"文件夹的内容,而是显示了根驱动器(注意:不是驱动器的内容).该文件夹确实存在,我也尝试使用其他文件夹.

When i run the application, instead of showing the content of the "/Users" Folder, it shows the root drive (note: not the content of the drive). The folder does exist and i also tried using other folders.

推荐答案

您是否尝试强制索引显示目录?

Did you try forcing the index to show the directory ?

listView->setRootIndex(dirModel->index("/Users"));

这对我来说很好:

#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QFileSystemModel model;
    model.setRootPath("/Users");
    QListView view;
    view.setModel(&model);
    view.setRootIndex(model.index("/Users/"));
    view.show();
    return app.exec();
}

这篇关于QFileSystemModel setRootPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆