Question

I am working on Qt 4.8.5 with Windows 7, and I have a SLOT that calls a modal QFileDialog, which is just like:

QFileDialog fileDialog(this);
fileDialog.setFileMode(QFileDialog::Directory);
if (fileDialog.exec()) {
    // blah blah
}

I've noticed that QFileDialog automatically keeps the recent path, and whenever it has been constructed, it's able to display the latest path even if I restart the main program.

But I have no idea about where Qt saves the status, and I'd like to get the latest path as a QString without opening a QFileDialog. Does anyone know how to achieve this?

Was it helpful?

Solution

I have extracted the code from Qt sources. As you can see, this information is stored in user scoped QSettings. On Windows, it is under the registry key: HKEY_CURRENT_USER\Software\Trolltech\OrganizationDefaults\Qt. And here is the code to use:

QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
settings.beginGroup(QLatin1String("Qt"));
QByteArray p = settings.value(QLatin1String("filedialog")).toByteArray();

QDataStream stream(&p, QIODevice::ReadOnly);
QByteArray splitterState;
QList<QUrl> bookmarks;
QStringList history;
QString currentDirectory;
qint32 marker;
qint32 v;

stream >> marker
       >> v
       >> splitterState
       >> bookmarks
       >> history
       >> currentDirectory; // <-- The recent path.

Please note, that this can be changed in the next Qt versions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top