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. How to extract a resource from qrc (Solved)
Forum Updated to NodeBB v4.3 + New Features

How to extract a resource from qrc (Solved)

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 13.0k Views 3 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.
  • F Offline
    F Offline
    fronald
    wrote on last edited by
    #1

    Hi,

    I need extract 1 file on first run of my desktop application. My resource is in a qrc.

    How do it?

    Thanks.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pkj__
      wrote on last edited by
      #2

      QFile::copy(":/path/to/qrc", "path/to/copy");
      copies the file from qrc to directory you specify. You can use other methods of QFile to load the file in memory.

      T 1 Reply Last reply
      1
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

        Resources are completely transparent to the application and they might be treated as regular files, for example for copying (see previous post). The only restriction is that they are read-only, both the files itself (you cannot modify them) and the directory strucuture they are contained in (you cannot add or remove files to or from a resource collection).

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fronald
          wrote on last edited by
          #4

          Qt is wonderful -

          My simple question have a simple answer.

          Thanks!

          1 Reply Last reply
          1
          • P pkj__

            QFile::copy(":/path/to/qrc", "path/to/copy");
            copies the file from qrc to directory you specify. You can use other methods of QFile to load the file in memory.

            T Offline
            T Offline
            thanhxp
            wrote on last edited by thanhxp
            #5

            @pkj__
            I did as you said but I cannot write a QT resource file to a directory-structure-path.
            Here is my code:

            #include <QCoreApplication>
            #include <QFile>
            #include <QString>
            #include <QDebug>
            #include <QTextStream>
            #include <iostream>
            
            using namespace std;
            
            void read(QString filename)
            {
                QFile file(filename);
                if(!file.open(QFile::ReadOnly |
                              QFile::Text))
                {
                    qDebug() << " Could not open the file for reading";
                    return;
                }
            
                QTextStream in(&file);
                QString myText = in.readAll();
            
                // put QString into qDebug stream
                qDebug() << myText;
            
                file.close();
            }
            
            int main(int argc, char *argv[])
            {
                QCoreApplication a(argc, argv);
            
                read(":/resources/hello.pro");
                bool status = QFile::copy(":/resources/hello.pro" , "~/abc/hel.pro");
                if(status) {
                        cout << "Success" << endl;
                    } else {
                        cout << "Failed" << endl;
                    }
            
            
                return 0;
            }
            

            Could you help me why I am getting Failed. I am expecting that I will create a new folder named abc inside the current directory and copy the resource file into that.
            Thank and best regards.

            Thanh C. Tran

            mrjjM 1 Reply Last reply
            0
            • T thanhxp

              @pkj__
              I did as you said but I cannot write a QT resource file to a directory-structure-path.
              Here is my code:

              #include <QCoreApplication>
              #include <QFile>
              #include <QString>
              #include <QDebug>
              #include <QTextStream>
              #include <iostream>
              
              using namespace std;
              
              void read(QString filename)
              {
                  QFile file(filename);
                  if(!file.open(QFile::ReadOnly |
                                QFile::Text))
                  {
                      qDebug() << " Could not open the file for reading";
                      return;
                  }
              
                  QTextStream in(&file);
                  QString myText = in.readAll();
              
                  // put QString into qDebug stream
                  qDebug() << myText;
              
                  file.close();
              }
              
              int main(int argc, char *argv[])
              {
                  QCoreApplication a(argc, argv);
              
                  read(":/resources/hello.pro");
                  bool status = QFile::copy(":/resources/hello.pro" , "~/abc/hel.pro");
                  if(status) {
                          cout << "Success" << endl;
                      } else {
                          cout << "Failed" << endl;
                      }
              
              
                  return 0;
              }
              

              Could you help me why I am getting Failed. I am expecting that I will create a new folder named abc inside the current directory and copy the resource file into that.
              Thank and best regards.

              Thanh C. Tran

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @thanhxp
              Hi
              Copy will not make a new folder
              You must do that yourself
              QDir().mkdir("~/abc");

              Update:
              https://forum.qt.io/topic/72915/copy-qt-resource-file-into-filesystem/2

              T 1 Reply Last reply
              2
              • mrjjM mrjj

                @thanhxp
                Hi
                Copy will not make a new folder
                You must do that yourself
                QDir().mkdir("~/abc");

                Update:
                https://forum.qt.io/topic/72915/copy-qt-resource-file-into-filesystem/2

                T Offline
                T Offline
                thanhxp
                wrote on last edited by
                #7

                @mrjj
                Thanks, problem solved.
                Best regards,
                Thanh C. Tran

                1 Reply Last reply
                1
                • JonBJ JonB referenced this topic on

                • Login

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