Results 1 to 3 of 3

Thread: QFileSystemModel: setRootPath() always shows me the same folder in Linux.

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default QFileSystemModel: setRootPath() always shows me the same folder in Linux.

    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?
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QFileSystemModel>
    5. #include <QMainWindow>
    6. #include <QtCore>
    7. #include <QtGui>
    8.  
    9. QT_BEGIN_NAMESPACE
    10. namespace Ui { class MainWindow; }
    11. QT_END_NAMESPACE
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. MainWindow(QWidget *parent = nullptr);
    19. ~MainWindow();
    20.  
    21. private slots:
    22. void on_buttonCompress_clicked();
    23.  
    24. void on_buttonExtract_clicked();
    25.  
    26. private:
    27. Ui::MainWindow *ui;
    28.  
    29. QFileSystemModel *fsModel;
    30. };
    31. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    5. {
    6. ui->setupUi(this);
    7.  
    8. const QString rootPath = "/media/backup/";
    9.  
    10. fsModel = new QFileSystemModel(this);
    11. fsModel->setRootPath(rootPath);
    12.  
    13. ui->treeView->setModel(fsModel);
    14. }
    15. //...
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileSystemModel: setRootPath() always shows me the same folder in Linux.

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: QFileSystemModel: setRootPath() always shows me the same folder in Linux.

    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.
    Qt Code:
    1. ui->treeView->setRootIndex(fsModel->index(rootPath));
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. Replies: 2
    Last Post: 22nd January 2015, 15:58
  2. Replies: 7
    Last Post: 13th February 2014, 07:23
  3. Replies: 0
    Last Post: 9th November 2013, 03:25
  4. Replies: 8
    Last Post: 12th July 2013, 02:00
  5. Replies: 3
    Last Post: 11th December 2012, 17:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.