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: include files from subfolders (Qt 5.5.1)
QtWS25 Last Chance

QFileSystemModel: include files from subfolders (Qt 5.5.1)

Scheduled Pinned Locked Moved Unsolved General and Desktop
qfilesystemmodeqt 5.5.1subdirs
15 Posts 2 Posters 6.9k 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.
  • Q Offline
    Q Offline
    qDebug
    wrote on last edited by
    #1

    Hello!

    Atm i use QFileSystemModel to get a filtered list of files and view them in a listview widget. But so far i fail on including files from subfolders as well. I did study the documentation up and down and google my way through a few 7 year old postings, but so far i did not find any example code how it may be possible to do this.

    For now i experiment with QDirIterator instead of QFileSystemModel but it is kinda slow and locks the app (depending on how much there is) for a few seconds. But i'm able to get all the files, at least in a nice qDebug output.

    Is there any working example out there on how to use QFileSystemModel and literate form a given root path through all subfolders? I'm using Qt 5.5.1.

    Thank you!

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

      Hi,

      QFileSystem is optimized to avoid consuming unneeded resources i.e. don't load everything at once so your use case looks like a candidate for a QDirIterator working in another thread and using signals and slots to send data upper in the chain.

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

      Q 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        QFileSystem is optimized to avoid consuming unneeded resources i.e. don't load everything at once so your use case looks like a candidate for a QDirIterator working in another thread and using signals and slots to send data upper in the chain.

        Q Offline
        Q Offline
        qDebug
        wrote on last edited by qDebug
        #3

        @SGaist
        Thank you, but at this point i don't even know how to add files to the model. Later on i want to give the user a choice to also search and include files in sub directories.

        files= new QFileSystemModel(this);
        files->setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::NoSymLinks);
        files->setNameFilters(myFilter);
        files->setNameFilterDisables(false);
        files->setRootPath(myPath);
        ui->listView1->setModel(files);
        ui->listView1->setRootIndex(files->setRootPath(myPath));
        

        If there are files i filter for, i get them listed in listView1. So far so good, but how to include also files from sub directories?

        QDirIterator subs(myPath, myFilter, QDir::Files, QDirIterator::Subdirectories);
        while(subs.hasNext())
        {
        	qDebug() << subs.next();
        }
        

        Thats the way i do it now but how can i add them to QFileSystemModel? There is no ui->listView1->append(subs.next()); or is there? I guess some basic stuff but i am unable to find a solution by myself so far.

        Thanks!

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

          You don't add paths to QFileSystemModel. That class represent your filesystem model starting at the root of the drive.

          QFileSystemModel will load additional data if you browse to folders you are interested in.

          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
          • Q Offline
            Q Offline
            qDebug
            wrote on last edited by
            #5

            So the only solution is to change models? If i want to include files from sub directories i have to change the model from QFileSystemModel to QStringListModel for example and use QDirIterator?

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

              What do you want to use it for ?

              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
              • Q Offline
                Q Offline
                qDebug
                wrote on last edited by
                #7

                The goal here is to find text files in a given path and list them in a QListView. If the user clicks on a text file in this list, he should be able to rename it and on a double click to edit the text file. So far so good, i got this covered.

                In some rare cases text files are sorted inside sub directories, one by one - means 20 text files are in 20 sub directories. It is kinda lame to click through all the subs so my idea was to add a button or check box to also include all text files in this given root path.

                The first part was easy and fast for me to accomplish but the second part - including text files from sub directories and add them to the QListView / QFileSystemModel is still a mystery to me. I'm still a Qt beginner, any help is appreciated.

                Thank you!

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

                  Then a model based on QAbstractListModel that you populate using QDirIterator might be the simple way to go.

                  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
                  • Q Offline
                    Q Offline
                    qDebug
                    wrote on last edited by
                    #9

                    This is not an option, i rather give up on this and show additional files in a log window or any other way. If the user browses through directories and clicks C: for example, the app will lock for seconds or minutes if no ssd is present.

                    Is it really that complicated to include sub directories in QFileSystemModel?

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

                      Just to be sure I understand you correctly, your additional folders should act like a QFileSystemModel with a 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
                      • Q Offline
                        Q Offline
                        qDebug
                        wrote on last edited by
                        #11

                        I just need them all together in the listview widget i got for files from the QFileSystemModel and i need the paths to the files so i can open them for edit.

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          qDebug
                          wrote on last edited by
                          #12

                          I sort of found a solution that works. On button click i change the listview model from QFileSystemModel to QStringListModel and call a function and use QDirIterator to add filenames to a QStringList. It needs additional code but if QFileSystemModel can't be set to a recursive directory mode, i don't have a lot of options, i guess.

                          Is there any better solution?

                          Thanks!

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

                            Like I wrote, QFileSystemModel is optimized to avoid filing up your memory with everything it can find on the hard drive.

                            Your use case really sounds like QDirIterator is a better choice to retrieve the information you want.

                            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
                            • Q Offline
                              Q Offline
                              qDebug
                              wrote on last edited by qDebug
                              #14

                              I don't need all the files from a hard drive, just the files in all the sub directories inside directory. For example on Windows: C:\text-files\2016*.txt and all the *.txt files from C:\text-files\2016\1*.txt C:\text-files\2016\2*.txt and so on. I don't need files from C:\Windows or C:\Programs or ...

                              Edit: Editor is removing \ before * so please imagine them there! ^^

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

                                That I understood. That's why I think that QDirIterator is a better tool to find what you want on your drive.

                                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

                                • Login

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