QSettings-文件选择器应记住上一个目录 [英] QSettings - File chooser should remember the last directory

查看:92
本文介绍了QSettings-文件选择器应记住上一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从某个位置上传文件,然后下一次上传必须指向最后上传的位置. 如何使用QSettings完成此操作?

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?

推荐答案

在使用QSettings之前,我建议在您的main()中设置一些有关您的应用程序和公司的信息,这些信息将QSettings将正在使用:

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();
}

然后,当使用QFile::getOpenFileName()选择文件时(例如),您可以从QSetting键读取最后一个目录.然后,如果所选文件有效,则可以存储/更新密钥的内容:

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 + "'");
    }
}

这篇关于QSettings-文件选择器应记住上一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆