PDA

View Full Version : QFileSystemModel: setRootPath() always shows me the same folder in Linux.



robgeek
20th January 2020, 23:28
Hello!

I'm trying to set a rootpath to see files and folders in a QTreeView but no matter what string I set to rootpath I always see the same root folder "/" in my case because I'm using linux.
I would like to set "/media/backup" but when I start the program it always shows my "/". Even if I pass to setRootPath("") it shows me the same folder.

Why and how can I fix this?

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QFileSystemModel>
#include <QMainWindow>
#include <QtCore>
#include <QtGui>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private slots:
void on_buttonCompress_clicked();

void on_buttonExtract_clicked();

private:
Ui::MainWindow *ui;

QFileSystemModel *fsModel;
};
#endif // MAINWINDOW_H


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);

const QString rootPath = "/media/backup/";

fsModel = new QFileSystemModel(this);
fsModel->setRootPath(rootPath);

ui->treeView->setModel(fsModel);
}
//...

d_stranz
21st January 2020, 03:03
Did you read the documentation of setRootPath(), particularly this part:


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.

The same model can be shared by multiple views, and each view may want to look at a different part of the model. So what you want is QTreeView::setRootIndex() to control where a particular view start its display in the model hierarchy.

robgeek
21st January 2020, 15:15
I've changed my code but now it starts showing the same root folder until "/rob/" but not the subfolders and thats what I want.

ui->treeView->setRootIndex(fsModel->index(rootPath));

All I want is show all "/home/rob/" subfolders and the possibility to access their subfolders too.