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 Qt resource file into filesystem
Forum Update on Monday, May 27th 2025

Copy Qt resource file into filesystem

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.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.
  • T Offline
    T Offline
    thanhxp
    wrote on last edited by A Former User
    #1

    Hi all,
    I want to copy a resource file into my current directory containing the executable file. The copy file should be in folder abc (not existed) inside my current directory.
    I am using QFile::copy() methods like this:

    #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;
    }
    

    The problem is that, when I run the executable file, it returns failed.
    I am expecting that it will create new directory abc inside my current folder then copy the resource file into that.
    Can anyone help me?

    Thanks and best regards.
    Thanh C. Tran

    jsulmJ 1 Reply Last reply
    0
    • T thanhxp

      Hi all,
      I want to copy a resource file into my current directory containing the executable file. The copy file should be in folder abc (not existed) inside my current directory.
      I am using QFile::copy() methods like this:

      #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;
      }
      

      The problem is that, when I run the executable file, it returns failed.
      I am expecting that it will create new directory abc inside my current folder then copy the resource file into that.
      Can anyone help me?

      Thanks and best regards.
      Thanh C. Tran

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @thanhxp said in Copy QT resource file into filesystem:

      I am expecting that it will create new directory abc inside my current folder

      That is a wrong assumption: copy() will not create any directories. You have to create it by yourself, see QDir (http://doc.qt.io/qt-5.7/qdir.html#mkdir).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      3
      • jsulmJ jsulm

        @thanhxp said in Copy QT resource file into filesystem:

        I am expecting that it will create new directory abc inside my current folder

        That is a wrong assumption: copy() will not create any directories. You have to create it by yourself, see QDir (http://doc.qt.io/qt-5.7/qdir.html#mkdir).

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

        @jsulm
        Thank you very much. I will redesign my program with QDir.
        Best regards.

        Thanh C. Tran

        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