Nominate our 2022 Qt Champions!

[SOLVED] How to create folder for save/get data in iOS by using widget code?

  • Hi,

    I can create folder on macbook pro and my android.
    But I can't create the folder on my iPad.
    I google it.
    I find it is hard to create a folder in the out side of the self-app's folder .
    Hence, I can't create the folder on the root.// It just my guest
    below code is my current code.
    How to fixed it?

        //get available folder
        QDir dir=QDir::rootPath();
        QString myPath=QDir::rootPath()+"sdcard";
        QDir* dir1=new QDir(myPath);
        if (!dir1->exists()) {
        if ("sdcard")) {
            QDir* dir2=new QDir(myPath+"/MyFile");
            if (!dir2->exists()) {

  • Lifetime Qt Champion


    You should use QStandardPaths::writableLocation to get a proper path to write to.

  • Hi, I tried the same thing (or think it is the same more or less) and tried to use the path QStandardPaths::writableLocation(QStandardPaths::DataLocation)+"filename.txt"). But this does not work.
    On android it was easier by using QDir::absolutePath()+"filename.txt". There has been created automatically such a file with this path, as there was no such one already before (so it worked).
    Is there a way to do it with QDir or something similar? Or what exaxtly would I need if I used QStandardPaths? (because I also tried ...Paths::ApplicationsLocation, ::DocumentsLocation)

    I hope you understand my problem.

  • Lifetime Qt Champion


    Aren't you missing a separator ? i.e. QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/filename.txt"

  • I already tried it and tried it now again. Does not work unfortunately.
    The path it gave me (when I used qDebug() << file.fielname) was:
    /var/mobile/Applications/262093E8-F9A7-4624-9559-FB3C6BF393E5/Library/Application Support/filename.txt
    Maybe the problem is that there is a space between Application and Support?

  • Lifetime Qt Champion

    No, that should not be a problem. What error do you get from your file ?

  • Well, the interesting thing is that I get no error, I only see that the data has not been saved after I restarted the app. What path would I use with qrc? Because I also tried it with that => path ":/data/filename.txt", that is the path qt also tells I should take, but this works only if I run it on my mac. Do I have to include something in the .pro file?

  • Lifetime Qt Champion

    Just to be sure I understand you correctly, do you want to read a file from qrc and store it on the device ?

  • yes, but I also like the other version if the data can be saved (sorry my english migth be unclear sometimes, I'm from switzerland)

  • Lifetime Qt Champion

    No no, your english is not that bad, it was just to confirm I understood you correctly.

    Since it's on iOS you may have to use the Q_INIT_RESOURCE macro to make your resources accessible.

  • Ok, I see it should work with that, but what line exactly do I have to add in main.cpp when I have Resources=>data.qrc=>/=>Database.txt or the "normal" path :/Database.txt? Because I tried many lines again and don't know where the error is.

  • Lifetime Qt Champion



    And then you can use the path like normal.

  • I don't know why, but it works now, I inserted the line in the main.cpp, but used the QStandardPaths::... Thank you very much!

  • Lifetime Qt Champion

    qrc is a read-only system by design. Otherwise it would mean that you can rewrite your executable in place.

    You're welcome !

    By the way, no need to modify the title to mark the thread as solved, the "Topic Tool" button is there for that :)