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. Splitting folders and sub-folders to add to tree
Forum Updated to NodeBB v4.3 + New Features

Splitting folders and sub-folders to add to tree

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 90.5k 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.
  • D Offline
    D Offline
    Dimbreath
    wrote on last edited by
    #1

    @ Folder1
    |
    |--- Folder2
    |--- Folder3
    |--- Folder4
    |--- Folder5
    |--- Folder6
    |--- Folder7
    |--- Folder8
    |--- Folder9@

    I need to split them so then i create the folders, so what i've tried is splitting the folders by "/". Leaving a QStringList like this: Folder3 subfolders = ("Folder4", "Folder5", "Folder8"). So then i do a loop and add them to the tree. But now if we look at Folder5 it has more sub-folders, so i would have to do the same process over and over until we don't find more "/". It can be an end-less process.

    This is what i've done:

    @ for(int counter = 0; counter < folders.length(); counter++) { // Loop though all folders.
    if(folders[counter].contains("/")) { // If the folders[counter] contains a "/"
    QStringList temp = folders[counter].split("/"); // We make a StringList splitting the "/", so, the temp has the sub-folders of the folders[counter]
    QTreeWidgetItem *mainFolder = m_ProjectTree->addFolder(m_ProjectTree->projectItem(), temp[0]); // We add the main Folder.
    for(int counter2 = 0; counter < temp.length(); counter2++) { // We loop though the sub-folders
    m_ProjectTree->addFolder(mainFolder, temp[counter2]); // We add the sub-folders to the tree.
    }
    }@

    (I feel like it's very tricky :()

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      As you already noted that may be an endless process. Even so it is in principle possible to program it in one routine it is not very elegant. Certainly you to define on the folder depth and things.

      This is a classical example for recursive routines. You need to write a routine considering only one folder and it sub-folders. When you have a sub-folder it becomes the folder for the next, recursive call to the very same routine and so on.

      "Here is a very general article":http://www.cplusplus.com/forum/articles/2231/ on recursion. This may help to understand recursion. "Here is something similar":http://www.qtcentre.org/threads/4307-how-to-recursive-a-directory-in-qt of what you are trying. Use google, there probably more and better cases already posted somewhere.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dimbreath
        wrote on last edited by
        #3

        Lets say i have a text file with all the folders: Text file contains this:

        @Folder1/Folder2 // Folder1 contains a sub-folder called Folder2.
        Folder1/Folder3 // Folder1 contains a sub-folder called Folder3.
        Folder1/Folder2/Folder4 // Folder1 contains a sub-folder called Folder2 that it contains a SubFolder Called4 @

        Would that make the process easier? If so, how it would be?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dimbreath
          wrote on last edited by
          #4

          Bump. :)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            What is your input?
            A file with the structure as shown in the first post?
            Or the file with the structure as shown in your second post?

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dimbreath
              wrote on last edited by
              #6

              What i mean is if i have all t he folders in a .txt file like this:

              @Folder1/Folder2 // Folder1 contains a sub-folder called Folder2.
              Folder1/Folder3 // Folder1 contains a sub-folder called Folder3.
              Folder1/Folder2/Folder4 // Folder1 contains a sub-folder called Folder2 that it contains a SubFolder Called4@

              And then having files listed like this:

              @Folder1/exe1.exe
              Folder1/Folder2/exe2.exe
              Folder1/Folder2/Icon.png
              Folder1/Folder2/Folder4/exe3.exe
              Folder1/Folder2/Folder4/Icon.png@

              Would ti make the process easier of creating folders and sub-folders?

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Dimbreath
                wrote on last edited by
                #7

                Bumpy. I still can't figure out how to do that? (It actually confuses me. :/)

                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