可以说我的系统中有以下文件夹:
/home/rob/musics/...
/home/rob/texts/...
/home/rob/images/...
我正在尝试创建一个qtreeview(我不知道这是否是最合适的小部件)以仅显示/ rob /目录中的文件夹/子文件夹和文件。但是问题是我的操作方式向我显示了根目录中的所有目录。
我想看的内容(文件和对子文件夹的访问):
/musics/...
/texts/...
/images/...
我得到的是:
/home/
/lib/
/root/
/usr/
/...
我不要那个!如何设置此文件系统的起点?
这是我尝试的:
// fsmodel is a QFileSystemModel
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
const QString rootPath = "/home/rob/";
fsModel = new QFileSystemModel(this);
fsModel->setRootPath(rootPath);
ui->treeView->setRootIndex(fsModel->index(rootPath));
ui->treeView->setModel(fsModel);
}
我正在使用linux。
最佳答案
如果执行代码,则应收到以下警告消息:
QAbstractItemView::setRootIndex failed : index must be from the currently set model
QAbstractItemView::setRootIndex failed : index must be from the currently set model
很明显,QTreeView还没有模型,但是您正在向它传递它不知道的模型的rootIndex。
解决方案是先设置模型,然后设置rootIndex:
ui->treeView->setModel(fsModel);
ui->treeView->setRootIndex(fsModel->index(rootPath));
关于c++ - 如何设置QTreeView的起点目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59920709/