0

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?

Jonas Stein
  • 6,826
  • 7
  • 40
  • 72
mrg95
  • 2,371
  • 11
  • 46
  • 89

1 Answers1

5

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/

Maxim Makhun
  • 2,197
  • 1
  • 22
  • 26