2

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.

Pawel
  • 169
  • 1
  • 12

1 Answers1

2

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));
}
willy
  • 487
  • 5
  • 21
  • As I said, that doesn't work as expected. Here's example: ![IMAGE](https://image.ibb.co/k61oMF/dialog.jpg) Here is what is being saved, and what is loaded to dialog (and no file is selected, cause name is not valid) – Pawel Mar 20 '17 at 09:46
  • Actually what is loaded to dialog should be correct. You see the selection on the text on your screenshot? Try pressing Home button and you will see that the input contains the full name of file. Really don't know if this is a Qt issue or the issue of standard file open dialog, as you are using the system one by default on windows. However you can override it with `DontUseNativeDialog` option. – Mikhail Churbanov Mar 20 '17 at 10:12
  • I think that is correct. Non native dialog shows correct file name (but oh my, is it ugly). So that must be a bug of native. However file is not selected like I would want to (like it was clicked), is there possible to scroll dialog to this file? And highlight it? – Pawel Mar 20 '17 at 11:17