QFileSystemModel and setRootPath
Unsolved
General and Desktop
-
Hey,
I was trying to show the files and folders in E:\A in a tree view using QFileSystemModel but for some reason, setRootPath doesn't seem to work and it keeps showing me the drives (C, D, E) and not the directory I wrote. Here's my code:mainwindow.cpp:
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); setWindowTitle("My App"); dirMdl = new QFileSystemModel(this); QString path = "E:/A"; dirMdl->setRootPath(path); ui->treeView1->setModel(dirMdl); } MainWindow::~MainWindow() { delete ui; }
mainwindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QtCore> #include <QtGui> #include <QMessageBox> #include <QtWidgets> #include <QString> #include <QTreeWidget> #include <QStringList> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); QFileSystemModel* dirMdl; private slots: private: Ui::MainWindow* ui; }; #endif // MAINWINDOW_H
-
It does what the documentation describes. There is also a note:
"Note: This function does not change the structure of the model or modify the data available to views. In other words, the "root" of the model is not changed to include only files and directories within the directory specified by newPath in the file system."
Pass the returned model index to https://doc.qt.io/qt-5/qabstractitemview.html#setRootIndex