如何设置目录分隔符以匹配操作系统?

发布于 2024-09-06 12:30:51 字数 576 浏览 4 评论 0原文

我正在编写一个 qt 应用程序,目标是可移植到 3 个主要操作系统。

我正在使用 QFileDialog 选择一个文件夹,然后将其添加到 QListWidget 中。然而,即使我在 Windows 上,文件夹名称也会返回为 E:/media。我希望它返回 E:\media

我可以使用简单的字符串替换,但在 Linux/Mac 上,

如果有帮助的话,拥有 \home\user\Documents 我的代码会看起来很奇怪:

void LibrariesForm::on_addButton_clicked()
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("Select Folder"), "/", QFileDialog::ShowDirsOnly);

    if (dir.isNull() == true)
    {
        return;
    }

    ui->librariesList->addItem(new QListWidgetItem(dir, ui->librariesList, 0));
}

I am writing a qt application, with the goal of it being portable to the 3 major operating systems.

I am using QFileDialog to select a folder, and then adding it to a QListWidget. However the folder name is being returned as E:/media even though I am on Windows. I would want it to return E:\media

I could use a simple string replace, but then on Linux/Mac it would look weird to have \home\user\Documents

My code if it helps:

void LibrariesForm::on_addButton_clicked()
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("Select Folder"), "/", QFileDialog::ShowDirsOnly);

    if (dir.isNull() == true)
    {
        return;
    }

    ui->librariesList->addItem(new QListWidgetItem(dir, ui->librariesList, 0));
}

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

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

发布评论

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

评论(2

零時差 2024-09-13 12:30:51

我猜您正在寻找 QDir::toNativeSeparators()

I guess you are looking for QDir::toNativeSeparators().

×纯※雪 2024-09-13 12:30:51

如果您仅在内部使用该字符串,则无需将斜杠转换为反斜杠。 Qt 的类也适用于 Linux 风格的路径。如果您想要一个“打印漂亮”的字符串,请采用 Jérôme 的答案。 :)

If you use the string just internally, you don't need to convert slashes to backslashes. Qt's classes work with linux-style pathes, too. If you want a "pretty printed" string, take Jérôme's answer. :)

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