Pawel
Pawel

Reputation: 169

getOpenFileName - preselect last opened not working

In my application I need to load many images. I would really use an option to select the last opened file, so I would like to know the last selected file. Documentation says:

QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                                            "/home",
                                            tr("Images (*.png *.xpm *.jpg)"));

The file dialog's working directory will be set to dir. If dir includes a file name, the file will be selected.

So I should save the last opened file path somewhere and then insert it instead of "/home". But that doesn't work. In the open file dialog, the file name is stripped to the few last characters and no file is selected. What is wrong? Is it a bug?

My current code:

QString fileName = QFileDialog::getOpenFileName(this, "Select file", 
    lastUsedFile, "Image Files (*.png *.jpg *.jpg *.bmp);; JPEG(*.jpg *.jpeg);; PNG(*.png);; BMP(*.bmp)");

if (!fileName.isEmpty())
    lastUsedFile = fileName;

What I would like to achieve is for the file to be to selected and scrolled to.

Upvotes: 2

Views: 706

Answers (1)

willy
willy

Reputation: 473

It's the third parameter of QFileDialog::getOpenFileName(). And then you have to store it in QSettings.

for example:

QString fileName = QFileDialog::getOpenFileName(
        this, tr("Open file"), 
        Settings.value(DEFAULT_DIR).toString(),
        tr("Images (*.png *.xpm *.jpg)");

if (!fileName.isEmpty()) {
   QDir curDir;
   Settings.setValue(DEFAULT_DIR, curDir.absoluteFilePath(fileName));
}

Upvotes: 2

Related Questions