QSettings - 文件选择器应该记住最后一个目录

发布于 2024-09-16 19:47:02 字数 61 浏览 4 评论 0原文

我从某个位置上传文件,然后下一次上传必须指向上次上传的位置。 我如何使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄风 2024-09-23 19:47:02

在使用QSettings之前,我建议在您的main()中设置一些有关您的应用程序和公司的信息,这些信息QSettings将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的键读取最后一个目录。然后,如果所选文件有效,您可以存储/更新密钥的内容:

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

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 + "'");
    }
}
香橙ぽ 2024-09-23 19:47:02

如果您正在谈论 QFileDialog(),您可以在构造函数中指定起始目录:

QFileDialog::QFileDialog(QWidget * parent = 0, const QString & caption = 
  QString(), const QString & directory = QString(), const QString & filter =
  QString())

或者您可以使用像这样的辅助函数之一,它也允许您指定起始目录:

QString QFileDialog::getOpenFileName(QWidget * parent = 0,
    const QString & caption = QString(), const QString & dir = QString(), 
    const QString & filter = QString(), QString * selectedFilter = 0, 
    Options options = 0)

每次使用后,存储原来的目录路径选择并在下次显示对话框时使用它。

If you are talking about QFileDialog() you can specify the starting directory in the constructor:

QFileDialog::QFileDialog(QWidget * parent = 0, const QString & caption = 
  QString(), const QString & directory = QString(), const QString & filter =
  QString())

Or you can use one of the helper functions like this one which also allow you to specify the starting directory:

QString QFileDialog::getOpenFileName(QWidget * parent = 0,
    const QString & caption = QString(), const QString & dir = QString(), 
    const QString & filter = QString(), QString * selectedFilter = 0, 
    Options options = 0)

After each use, store the directory path that was selected and use it the next time you display the dialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文