Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

History of multiple instances of QFileDialog

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.

like image 984
Jackzz Avatar asked Oct 30 '22 00:10

Jackzz


1 Answers

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();
}
like image 107
Bastien Thonnat Avatar answered Nov 16 '22 15:11

Bastien Thonnat