【问题标题】:QFileSystemModel setRootPathQFileSystemModel setRootPath
【发布时间】:2012-09-03 16:16:03
【问题描述】:

我正在尝试创建一个显示文件夹内容的 Qt 应用程序(Mac OS 中的“用户”文件夹)。 代码如下:

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

ui->listView->setModel(dirModel);

我也尝试过使用this code

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

【问题讨论】:

  • 您是从您链接的问题中还是从它的答案中尝试了代码? (我只是对其进行了编辑以使其更清晰)。
  • 我尝试了问题中的代码。我现在将尝试答案
  • setRootPath 在路径上安装文件系统观察程序以反映更改。正如下面的答案所述,需要setRootIndexdoc.qt.io/qt-5/qfilesystemmodel.html#setRootPath

标签: c++ macos qt qtreeview qfilesystemmodel


【解决方案1】:

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

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

【讨论】:

  • 我得到一个错误:QAbstractItemView::setRootIndex failed : index must be from the current set model
【解决方案2】:

这段代码也对我有用:

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

ui->listView->setModel(dirModel);
ui->listView->setRootIndex(dirModel->setRootPath("/Users"));

【讨论】:

    猜你喜欢
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多