c++windowsqtqstandardpaths

Qt How can I use DocumentsLocation


I am trying to develop an application with Qt. My problem is: I need to write and delete something in a text file. I am writing the text file as full path, the path on my computer. If the application runs on another computer, it will not find this path. I found out that I can use QStandardPaths::DocumentsLocation for its solution. But I couldn't figure out how to use it. Can you teach me or give an example?


Solution

  • You can get Documents location by below code:

    #include <QGuiApplication>
    #include <QDebug>
    #include <QStandardPaths>
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        qDebug() << QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
        return app.exec();
    }
    

    This code prints current user's Documents Location. You can store this folder location in QString and then use it as you need to store your files.

    Reference: QStandardPaths::StandardLocation