Important: Please read the Qt Code of Conduct -

How to pass a filepath with backslash "\" instead of forwardslash "/" ?

  • Hi guys,
    I have an application where I choose a file and the application picks this filepath and records in a .ini file that will be read by another application later. The thing is, that this other application was made by another person, in another programming language, so it reads the filepath with "" instead of "/". How do I change the filepath to output the filepath with "", instead of "/" ?
    Thanks in advance.

  • Hello,

    Look in the QDir class for NativeSeparators.


  • Lifetime Qt Champion

    @Flavio-Mesquita You can simply replace / with \

    QString path = "c:/a/b/c";
    QString modifiedPath = path.replace('/', '\\');

  • @mad-hatter Thanks for the answer. So in my application instead of defining my settings like this:

    QSettings s;
    auto const defaultPath = onWindows ? QDir::currentPath() : QDir::homePath();

    I should use it like that:

    QSettings s;
    auto const defaultPath = onWindows ? QDir::toNativeSeparators(currentPath()) : QDir::toNativeSeparators(homePath());

    Or should I add something else? More arguments?

    Sorry for the dumb question, but I´m new to programming,never done something like this before.

  • @jsulm Thanks. But I´m picking the path straight from a lineEdit, like this:
    impedance_file = ui.impedanceApp->text();, then this variable impedance_file is recorded on the .ini file.
    So I will have to change the "/" for "\ " before recording i think. Since Qt picks the filepath as a text with / separators. Am I right?

  • @Flavio-Mesquita
    It's up to you whether you do your conversion of those separators before you write the result into a .ini file or via QSettings, or whether you do it as you read back in/use stuff. It doesn't really matter, so long as you are consistent with your reads & writes.

    But I´m picking the path straight from a lineEdit, like this:

    Depending on your usage, you might like to look at QFileDialog, which provides a way for users to pick a file path rather than typing one in? Then you usually don't have path-separator issues.

  • @JonB Thanks JonB, l didnt´t explain it well, I have QFileDialog with a lineEdit like this:

    QString Interface2::impedancePath() const
    return QFileInfo(ui.impedanceApp->text()).absolutePath();

    void Interface2::browseImpedanceApp()
    if (!m_fileDialog2) {
    m_fileDialog2 = new QFileDialog(this);
    connect(m_fileDialog2, &QFileDialog::fileSelected, ui.impedanceApp, &QLineEdit::setText);
    connect(m_fileDialog2, &QFileDialog::fileSelected, this, &Interface2::saveSettings);
    m_fileDialog2->setWindowTitle(tr("Select the impedance file"));

    Then when i click the RUN button:

    void Interface2::run()

    impedance_file = ui.impedanceApp->text();

    bool saved = false;
    auto const iniFileName = QStringLiteral("%1/sismica1.ini").arg(sismicaPath());
    QSaveFile file(iniFileName);
    if ( | QIODevice::Text)) {
        saved = file.commit();
     if (!saved)
        QMessageBox::warning(this, tr("Sismica"), tr("Error: Unable to save the file \"%1\".").arg(iniFileName));


    I´m not typing the path, from what I understood the QFileDialog picks the path with "/" as standard. What is correct, the application that reads my .ini file is the one that is wrong in my opinion, but I can´t change that one.

  • @Flavio-Mesquita
    OK, good that are using QFileDialog already, Yes, it returns paths with / rather than \s.

    I'm now a little lost as to which filename you are talking about with these characters. Your impedance_file = ui.impedanceApp->text() refers to a string path entry you want included in the content of the .ini file you are creating, right? Not to the path you want to save the .ini file itself as, right?

    Assuming it is indeed an entry in the .ini file you want, like impedance_file = C:\some\file\path, and if you are saying this .ini file must be readable by another app which wants \s and not /s when it sees that line, then yes you should map /s to \s before you write to file, and unmap if you read that file.

  • @JonB u got it. It is exactly what i want, just don´t know how to do.

  • Thanks Jsulm and JonB, got it working, I did it work changing as Jsulm said before recording as JonB said.

Log in to reply