如何设置目录分隔符以匹配操作系统?
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜您正在寻找 QDir::toNativeSeparators()。
I guess you are looking for QDir::toNativeSeparators().
如果您仅在内部使用该字符串,则无需将斜杠转换为反斜杠。 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. :)