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. [SOLVED]QFileSystemModel set root path

[SOLVED]QFileSystemModel set root path

Scheduled Pinned Locked Moved General and Desktop
20 Posts 5 Posters 14.3k Views 1 Watching
  • 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.
  • saeedhardanS Offline
    saeedhardanS Offline
    saeedhardan
    wrote on last edited by
    #1

    hi,
    I'm trying to set the root path to a specific directory , but it doesn't work .
    this is my code :
    @
    QString fPath = "APP_sessions/";
    files = new QFileSystemModel(this);
    QStringList filters;

    filters << "*.C";
    filters << "*.sqlite";
    
    files->setNameFilters(filters);
    files->setNameFilterDisables(false);
    if(QDir(fPath).exists()){
        QWidget tmp;
        QMessageBox::information(&tmp,"exist","exist");
    }
    files->setRootPath(fPath);
    

    @
    the message box (for testing purposes) pops out which means that qt recognizes the directory , but the treeview starts with the c: drive and lists only the drives on the machine (c: d: ...).
    Im on windows Qt .

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You should rather give the full path to setRootPath. I haven't check but setting a relative path to QFileSystemModel doesn't sound like the right thing to do.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • saeedhardanS Offline
        saeedhardanS Offline
        saeedhardan
        wrote on last edited by
        #3

        this is the full path :
        @
        QString fPath = "C:\Users\saeedh\QT_projects\home_work\build-ACS_tool-Desktop_Qt_5_3_MSVC2013_32bit-Debug\APP_sessions";
        @
        its without spaces and doen't work , this time it doesn't even pop the message which means it doesn't recognize the directory .

        1 Reply Last reply
        0
        • V Offline
          V Offline
          Vovasty
          wrote on last edited by
          #4

          Hi,
          maybe following link helps:
          "Link":http://qt-project.org/forums/viewthread/1846

          1 Reply Last reply
          0
          • saeedhardanS Offline
            saeedhardanS Offline
            saeedhardan
            wrote on last edited by
            #5

            [quote author="Vovasty" date="1413279412"]Hi,
            maybe following link helps:
            "Link":http://qt-project.org/forums/viewthread/1846[/quote]
            sorry but my problem is treeview doesn't even set my directory , i dont care if other directories also displayed .

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              It's an invalid path. A backslash in a strings is an escape character. Either escape your backslashes (use \ in your string) or since you are using Qt, use the unix notation: forward slashes, less error prone.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • saeedhardanS Offline
                saeedhardanS Offline
                saeedhardan
                wrote on last edited by
                #7

                [quote author="SGaist" date="1413279766"]It's an invalid path. A backslash in a strings is an escape character. Either escape your backslashes (use \ in your string) or since you are using Qt, use the unix notation: forward slashes, less error prone.[/quote]

                it helped with recognizing the directory (the message now pops up) , but not with the root tree view path (back as original post problem)

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Salvatello
                  wrote on last edited by
                  #8
                  QFileSystemModel* fsModel = new QFileSystemModel(this);
                  fsModel->setRootPath("c:/file qt/");
                  fsModel->setReadOnly(true);
                  fsModel->setFilter(QDir::AllDirs | QDir::AllEntries |QDir::NoDotAndDotDot);
                  QStringList filter;
                  filter<<"*.txt";
                  fsModel->setNameFilters(filter);
                  
                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Salvatello
                    wrote on last edited by
                    #9

                    QFileSystemModel* fsModel = new QFileSystemModel(this).
                    fsModel->setRootPath("c:/file qt/");
                    fsModel->setReadOnly(true);
                    fsModel->setFilter(QDir::AllDirs | QDir::AllEntries |QDir::NoDotAndDotDot);
                    QStringList filter;
                    filter<<"*.txt";
                    fsModel->setNameFilters(filter);

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      You need to also call setRootIndex in your tree view. You have the details in the documentation from QFileSystemModel

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Salvatello
                        wrote on last edited by
                        #11

                        *@@

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Salvatello
                          wrote on last edited by
                          #12

                          @QFileSystemModel* fsModel = new QFileSystemModel(this);
                          fsModel->setRootPath("c:/file qt/");
                          fsModel->setReadOnly(true);
                          fsModel->setFilter(QDir::AllDirs | QDir::AllEntries |QDir::NoDotAndDotDot);
                          QStringList filter;
                          filter<<"*.txt";
                          fsModel->setNameFilters(filter);@

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            No need to post your code again, you can simply edit your answer to add the coding tags

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • saeedhardanS Offline
                              saeedhardanS Offline
                              saeedhardan
                              wrote on last edited by
                              #14

                              [quote author="SGaist" date="1413280688"]You need to also call setRootIndex in your tree view. You have the details in the documentation from QFileSystemModel[/quote]

                              now it works , thanks a lot , all i added was :
                              @
                              ui->treeView->setRootIndex(files->index(fPath));
                              @
                              after you set the tree view model

                              1 Reply Last reply
                              1
                              • S Offline
                                S Offline
                                Salvatello
                                wrote on last edited by
                                #15

                                I'm sorry . are new .

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  You're welcome !

                                  No problem, everybody falls for that one ;)

                                  Happy coding !

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    Salvatello
                                    wrote on last edited by
                                    #17

                                    If you are using treeview can also try qDirModel
                                    @ QStringList filters;
                                    filters << "*.txt";
                                    model = new QDirModel(this);
                                    model->setReadOnly(false);
                                    model->setReadOnly(true);
                                    model->setSorting(QDir::DirsFirst |QDir::IgnoreCase | QDir::Type);
                                    model->setFilter(QDir::AllDirs | QDir::AllEntries |QDir::NoDotAndDotDot);
                                    model->setNameFilters(filters);

                                    ui->treeViewDirectory->setModel(model);
                                    QModelIndex index=model->index("C:/file qt/");
                                    ui->treeViewDirectory->expand(index);
                                    ui->treeViewDirectory->scrollTo(index);@
                                    
                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      QDirModel has been obsoleted, QFileSystemModel is the right class to use

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        Salvatello
                                        wrote on last edited by
                                        #19

                                        ok ok

                                        1 Reply Last reply
                                        0
                                        • terma.abaT Offline
                                          terma.abaT Offline
                                          terma.aba
                                          wrote on last edited by
                                          #20

                                          What if the view is QML ListView and has no setRootIndex()?

                                          QFileSystemModel is badly designed, it makes wrong assumptions on functions you have to be able to call on the view.

                                          1 Reply Last reply
                                          0

                                          • Login

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