1

I'm loading some files for my project....but every time the fileDialog directory is my root directory...

I want the fileDialog to remember my path and I've tried the solution in the following link qsettings-file-chooser-should-remember-the-last-directory but this worked for me for the same session only.

Is there a way to save the directory for other sessions?(when closing the application and re-opening it)?

Tamim Boubou
  • 75
  • 11
  • Try saving the value into an ini file. And load it back when you reopen the app. – xanadev May 15 '18 at 10:17
  • I 'm not sure if I know how to do it, I'm fairly new with Qt but I'll try... – Tamim Boubou May 15 '18 at 10:24
  • The link you provided in your question gives you everything you need to persist this information accross runs. Correctly set your app information in your main() function, then you can simply uses a QSettings instance to persist the information. – Antwane May 15 '18 at 10:25
  • What app information should I set ?...I couldn't find anything related – Tamim Boubou May 15 '18 at 10:34

1 Answers1

3

You can use the QSettings class.
This is simple example:
widget.h

#define WIDGET_H

#include <QWidget>
#include <QSettings>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

private:
    QString lastDir;
    QSettings *settings;
    void settingsLoader();
    void settingsSaver();
};

#endif // WIDGET_H

widget.cpp

#include <QFileDialog>
#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    settings = new QSettings("MyCompany", "My soft name", this);
    settingsLoader();
    lastDir = QFileDialog::getExistingDirectory(this, tr("Open directory"), lastDir);
}

void Widget::settingsLoader()
{
    lastDir = settings->value("LastDir", QDir::homePath()).toString();
}

void Widget::settingsSaver()
{
    settings->setValue("LastDir", lastDir);
}

Widget::~Widget()
{
    settingsSaver();
}
eska2000
  • 113
  • 8
  • I'm not sure I know what's the difference between the code in my question and your answer but it worked ! Thanks a lot.. – Tamim Boubou May 15 '18 at 12:41