How to save a file from Qrc to application directory

518 Views Asked by At

I am using Qt 5.15.1.

If I have a file in in my Qt app's qrc file, is it possible to save the file into application's directory so that I can use that file as a normal file on the file system?

QFile my_file(":/data/file.txt");
QDir my_app_dir(QCoreApplication::applicationDirPath());

in above code, how can I save my_file into my_app_dir?

Environment:
MacOS Catalina
Qt: Commercial version 5.15.1

1

There are 1 best solutions below

8
eyllanesc On

You just have to copy the content:

QFile my_file(":/data/file.txt");
QDir my_app_dir(QCoreApplication::applicationDirPath());
QString new_path = my_app_dir.absoluteFilePath("file.txt");
if(!my_file.copy(new_path)){
    qDebug() << my_file.error() << my_file.errorString();
}