How to set the directory separator character to match the operating system?

一曲冷凌霜 提交于 2019-12-12 17:28:55

问题


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

回答1:


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




回答2:


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. :)



来源:https://stackoverflow.com/questions/3107620/how-to-set-the-directory-separator-character-to-match-the-operating-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!