Search code examples
c++qtdirectoryappdataqfiledialog

QFileDialog to default to AppData?


In my program, I have a button that opens a getOpenFileName dialog like this:

path = QFileDialog::getOpenFileName(this, tr("Select region"), "%APPDATA%", tr("region file"));

I want the dialog to default to the users AppData folder. All users are running Windows (XP or higher). How could I ensure it defaults to AppData?


Solution

  • You can use QDesktopServices static method (Qt4):

    QString path = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
    

    In Qt5:

    QString path = QStandardPaths::standardLocations(QStandardPaths::DataLocation).at(0);
    

    To get Roaming folder:

    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "AppName", "application");
    QString location = QFileInfo(settings.fileName()).absolutePath() + "/";
    

    In my case path was:

    C:/Users/maxim.makhun/AppData/Roaming/AppName/