5

I am using QT in my C++ application. I know that when I use QFileDialog, the history is saved in the registry. Suppose I have multiple instances of QFileDialog within the application. Can I save the history for each instance separately? As far as I checked, it seems that same registry entry is updated for each instance.

Jackzz
  • 1,417
  • 4
  • 24
  • 53

1 Answers1

2

You could use different QSettings entry for each QFileDialog instance, with that you manage your history lenght and location.

something like that

void callFileDialog(QLinkedList<QString> & fileDialogHistory)
{
    QString fileName =  QFileDialog::getOpenFileName(Q_NULLPTR, "Open File", 
    QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
    if(!fileName.isNull()){
        fileDialogHistory << fileName;
    }
}

void saveFileDialogHistory(QLinkedList<QString> & fileDialogHistory, QString 
fileDialogHistoryName = "History_Default")
{
    QSettings settings;
    settings.beginWriteArray(fileDialogHistoryName);
    int index = 0;
    for (QLinkedList<QString>::iterator it = fileDialogHistory.begin(); it != fileDialogHistory.end(); ++it){
        settings.setArrayIndex(index);
        settings.setValue("filePath", QFileInfo(*it).filePath());
        index++;
    }
    settings.endArray();
}
Bastien Thonnat
  • 563
  • 4
  • 9