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. copy file from resource to disk keeping folder structure (platform independant)
QtWS25 Last Chance

copy file from resource to disk keeping folder structure (platform independant)

Scheduled Pinned Locked Moved Unsolved General and Desktop
qrcqfileresourcecopy
7 Posts 5 Posters 5.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.
  • CybeXC Offline
    CybeXC Offline
    CybeX
    wrote on last edited by CybeX
    #1

    Problem:

    How do I copy resource files, while preserving file names, to the disk in a platform independent way?

    Example and Explanation:

    I have files in my Resource File (.qrc file) e.g.:

    :/student/stud_temp              (student/template.txt)
    :/student/examples/stud_exOne    (student/examples/student_example1.txt)
    :/student/examples/stud_exTwo    (student/examples/student_example2.txt)
    
    :/lecturer/lec_temp              (lecturer/template.txt)
    :/lecturer/lec_ex                (lecturer/lecturer_example.txt)
    :/lecturer/data/lec_data         (lecturer/data/data_file.dat)
    

    I would like to copy these files on to the disk, for sake of the example, preserve the file structure.

    So the resulting folder structure should be something like this (unix system):

    $ ls $PWD
    student lecturer
    
    $ ls $PWD/student
    template.txt
    examples
    
    $ ls $PWD/lecturer
    template.txt
    lecturer_example.txt
    data
    

    There is a method suggested here to copy to filesystem, as shown below, but the file name needs to be specified. However this should be automated to get the resource's original filename.

    Currently I need to do the following to replicate the folder structure:

    QDir::mkpath("../student");    
    QFile::copy(":/student/template.txt", "../student/template.txt")
    

    However I cannot believe that this is the best way.

    Also, this method is platform dependant, due to the directory separators (trivial problem though).

    Are there any better ways to achieve this?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      Hi @cybex

      In my opinion, using the working directory is a bad idea.
      You should better use one of the standard locations defined in the QStandardPaths class instead.

      CybeXC 1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @CybeX said

        However I cannot believe that this is the best way.

        Looks ok. What's wrong with it?

        @CybeX said

        Also, this method is platform dependant, due to the directory separators (trivial problem though).

        Qt uses / as universal separator and translates it to the platform specific ones. You don't have to worry about it yourself.

        What I would worry about though is the usage of ... This is relative to the working directory, which will vary depending on how your app was started. Use applicationDirPath() and make your paths relative to that.

        CybeXC 1 Reply Last reply
        1
        • M mpergand

          Hi @cybex

          In my opinion, using the working directory is a bad idea.
          You should better use one of the standard locations defined in the QStandardPaths class instead.

          CybeXC Offline
          CybeXC Offline
          CybeX
          wrote on last edited by
          #4

          @mpergand thanks for the suggestion, although the use of the current directory is just an example to illustrate my question/(issue). I will be using the QTemporaryDir due to the nature of the files I am using.

          1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            @CybeX said

            However I cannot believe that this is the best way.

            Looks ok. What's wrong with it?

            @CybeX said

            Also, this method is platform dependant, due to the directory separators (trivial problem though).

            Qt uses / as universal separator and translates it to the platform specific ones. You don't have to worry about it yourself.

            What I would worry about though is the usage of ... This is relative to the working directory, which will vary depending on how your app was started. Use applicationDirPath() and make your paths relative to that.

            CybeXC Offline
            CybeXC Offline
            CybeX
            wrote on last edited by
            #5

            @Chris-Kawa @mpergand

            Looks ok. What's wrong with it?

            Well...nothing but it is an extremely tedious process...

            Ok, let me change the question slightly here to help solve this using a different method.

            Can (and how) am I able to get the original resource file name (e.g. below) from the QResource object.

            template.txt
            

            or

            student/template.txt
            

            I attempted this by using the QResource::fileName() and QResource::absoluteFileName() both return the resource location as a string shown below:

            :/student/stud_temp
            

            Thoughts?

            benlauB 1 Reply Last reply
            0
            • CybeXC CybeX

              @Chris-Kawa @mpergand

              Looks ok. What's wrong with it?

              Well...nothing but it is an extremely tedious process...

              Ok, let me change the question slightly here to help solve this using a different method.

              Can (and how) am I able to get the original resource file name (e.g. below) from the QResource object.

              template.txt
              

              or

              student/template.txt
              

              I attempted this by using the QResource::fileName() and QResource::absoluteFileName() both return the resource location as a string shown below:

              :/student/stud_temp
              

              Thoughts?

              benlauB Offline
              benlauB Offline
              benlau
              Qt Champions 2016
              wrote on last edited by
              #6

              @CybeX said in copy file from resource to disk keeping folder structure (platform independant):

              @Chris-Kawa @mpergand

              Looks ok. What's wrong with it?

              Well...nothing but it is an extremely tedious process...

              Ok, let me change the question slightly here to help solve this using a different method.

              Can (and how) am I able to get the original resource file name (e.g. below) from the QResource object.

              Why don't you just give up to set alias and use the original file name in qrc?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                saurabhjadhav1911
                wrote on last edited by
                #7
                //copy whole <from> directory to the folder <to> 
                void copyDirectoryNested(QString from,QString to)
                {
                
                    QDirIterator it(from, QDirIterator::Subdirectories);
                
                    while (it.hasNext()) {
                
                        QString file_in = it.next();
                
                        QFileInfo file_info = QFileInfo(file_in);
                
                        QString file_out = file_in;
                        file_out.replace(from,to);
                
                        if(file_info.isFile())
                        {
                            //is file copy
                            qDebug() << QFile::copy(file_in, file_out);
                            qDebug() << file_in << "<----" << file_out;
                        }
                
                        if(file_info.isDir())
                        {
                            //dir mkdir
                            QDir dir(file_out);
                            if (!dir.exists())
                                qDebug() << "mkpath"<< dir.mkpath(".");
                        }
                    }
                }
                

                then use above function :

                copyDirectoryNested(":/<qrc_folder_location>",<writing_location>);
                
                copyDirectoryNested(":/DefaultAppData",app_data_location);
                
                1 Reply Last reply
                2

                • Login

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