Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

qt

qfiledialog

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));
}
like image 662
esac Avatar asked Sep 03 '25 04:09

esac


1 Answers

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

like image 133
Jérôme Avatar answered Sep 05 '25 00:09

Jérôme