Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
754 views
in Technique[技术] by (71.8m points)

qt - QSettings - File chooser should remember the last directory

I upload a file from a location, then the next upload has to point the last uploaded location. How can I accomplish thus using QSettings?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Before using QSettings, I would suggest, in your main() to set a few informations about your application and your company, informations that QSettings will be using :

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.setApplicationName("test");
    a.setOrganizationName("myorg");
    a.setOrganizationDomain("myorg.com");

    // etc...
    return a.exec();
}

Then, when selecting a file with QFile::getOpenFileName()(for instance), you can read from a key of QSetting the last directory. Then, if the selected file is valid, you can store/update the content of the key :

void Widget::on_tbtFile_clicked() {
    const QString DEFAULT_DIR_KEY("default_dir");

    QSettings MySettings; // Will be using application informations
                          // for correct location of your settings

    QString SelectedFile = QFileDialog::getOpenFileName(
        this, "Select a file", MySettings.value(DEFAULT_DIR_KEY).toString());

    if (!SelectedFile.isEmpty()) {
        QDir CurrentDir;
        MySettings.setValue(DEFAULT_DIR_KEY,
                            CurrentDir.absoluteFilePath(SelectedFile));

        QMessageBox::information(
            this, "Info", "You selected the file '" + SelectedFile + "'");
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...