Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to pass a filepath with backslash "\" instead of forwardslash "/" ?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 5.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Flavio Mesquita
    wrote on 28 Jun 2018, 11:51 last edited by Flavio Mesquita
    #1

    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.

    J 1 Reply Last reply 28 Jun 2018, 12:13
    0
    • M Offline
      M Offline
      mad-hatter
      wrote on 28 Jun 2018, 12:00 last edited by
      #2

      Hello,

      Look in the QDir class for NativeSeparators.

      Regards

      F 1 Reply Last reply 28 Jun 2018, 12:29
      5
      • F Flavio Mesquita
        28 Jun 2018, 11:51

        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.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 28 Jun 2018, 12:13 last edited by
        #3

        @Flavio-Mesquita You can simply replace / with \

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        F 1 Reply Last reply 28 Jun 2018, 12:50
        2
        • M mad-hatter
          28 Jun 2018, 12:00

          Hello,

          Look in the QDir class for NativeSeparators.

          Regards

          F Offline
          F Offline
          Flavio Mesquita
          wrote on 28 Jun 2018, 12:29 last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • J jsulm
            28 Jun 2018, 12:13

            @Flavio-Mesquita You can simply replace / with \

            QString path = "c:/a/b/c";
            QString modifiedPath = path.replace('/', '\\');
            
            F Offline
            F Offline
            Flavio Mesquita
            wrote on 28 Jun 2018, 12:50 last edited by
            #5

            @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?

            J 1 Reply Last reply 28 Jun 2018, 12:55
            0
            • F Flavio Mesquita
              28 Jun 2018, 12:50

              @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?

              J Offline
              J Offline
              JonB
              wrote on 28 Jun 2018, 12:55 last edited by JonB
              #6

              @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.

              F 1 Reply Last reply 28 Jun 2018, 13:24
              0
              • J JonB
                28 Jun 2018, 12:55

                @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.

                F Offline
                F Offline
                Flavio Mesquita
                wrote on 28 Jun 2018, 13:24 last edited by
                #7

                @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->setAcceptMode(QFileDialog::AcceptOpen);
                m_fileDialog2->setFileMode(QFileDialog::ExistingFile);
                m_fileDialog2->setWindowTitle(tr("Select the impedance file"));
                }
                m_fileDialog2->setDirectory(impedancePath());
                m_fileDialog2->show();
                }

                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 (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
                    file.write(initFileContents(sp));
                    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.

                J 1 Reply Last reply 28 Jun 2018, 13:35
                0
                • F Flavio Mesquita
                  28 Jun 2018, 13:24

                  @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->setAcceptMode(QFileDialog::AcceptOpen);
                  m_fileDialog2->setFileMode(QFileDialog::ExistingFile);
                  m_fileDialog2->setWindowTitle(tr("Select the impedance file"));
                  }
                  m_fileDialog2->setDirectory(impedancePath());
                  m_fileDialog2->show();
                  }

                  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 (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
                      file.write(initFileContents(sp));
                      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.

                  J Offline
                  J Offline
                  JonB
                  wrote on 28 Jun 2018, 13:35 last edited by
                  #8

                  @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.

                  F 1 Reply Last reply 28 Jun 2018, 13:43
                  2
                  • J JonB
                    28 Jun 2018, 13:35

                    @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.

                    F Offline
                    F Offline
                    Flavio Mesquita
                    wrote on 28 Jun 2018, 13:43 last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      Flavio Mesquita
                      wrote on 28 Jun 2018, 14:12 last edited by
                      #10

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

                      1 Reply Last reply
                      1

                      2/10

                      28 Jun 2018, 12:00

                      topic:navigator.unread, 8
                      • Login

                      • Login or register to search.
                      2 out of 10
                      • First post
                        2/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved