Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileSystemModel and setRootPath
Forum Updated to NodeBB v4.3 + New Features

QFileSystemModel and setRootPath

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 193 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    ParsaAI
    wrote on last edited by
    #1

    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
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved