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. QFile path's toNativeSeparators returns wrong value on separator
Forum Updated to NodeBB v4.3 + New Features

QFile path's toNativeSeparators returns wrong value on separator

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 824 Views
  • 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.
  • Puppy BearP Offline
    Puppy BearP Offline
    Puppy Bear
    wrote on last edited by Puppy Bear
    #1

    HI everyone
    I'm trying to set a path with this function
    void Save_DB::set_new_path(QString path)
    {
    new_path = QDir::toNativeSeparators(path);
    if (!QFile(new_path).exists())
    qDebug() << "file not exist!\n";
    return;
    }
    and try to examine it with my own file.
    I'm pretty sure that there's something wrong with tonativeSeparators

    my input is
    D:/XL_db.db
    and the QFile.exists() returns false
    I've printed it which shows
    D:XL_db.db

    any idea of convertion of different separators like '' '//' '/' '\' in win10 msvc2015 qt4.9?

    J.HilkJ jsulmJ KroMignonK 3 Replies Last reply
    0
    • Puppy BearP Puppy Bear

      HI everyone
      I'm trying to set a path with this function
      void Save_DB::set_new_path(QString path)
      {
      new_path = QDir::toNativeSeparators(path);
      if (!QFile(new_path).exists())
      qDebug() << "file not exist!\n";
      return;
      }
      and try to examine it with my own file.
      I'm pretty sure that there's something wrong with tonativeSeparators

      my input is
      D:/XL_db.db
      and the QFile.exists() returns false
      I've printed it which shows
      D:XL_db.db

      any idea of convertion of different separators like '' '//' '/' '\' in win10 msvc2015 qt4.9?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #2

      @Puppy-Bear if you're using QFile, than there is no need for toNativeSeparators QFile operates on normal forwards slashes and handles those automatically correctly, when talking to system level functions/apis


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      Puppy BearP 1 Reply Last reply
      1
      • Puppy BearP Puppy Bear

        HI everyone
        I'm trying to set a path with this function
        void Save_DB::set_new_path(QString path)
        {
        new_path = QDir::toNativeSeparators(path);
        if (!QFile(new_path).exists())
        qDebug() << "file not exist!\n";
        return;
        }
        and try to examine it with my own file.
        I'm pretty sure that there's something wrong with tonativeSeparators

        my input is
        D:/XL_db.db
        and the QFile.exists() returns false
        I've printed it which shows
        D:XL_db.db

        any idea of convertion of different separators like '' '//' '/' '\' in win10 msvc2015 qt4.9?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Puppy-Bear said in Qt's toNativeSeparators returns wrong value on separator:

        I've printed it which shows

        How?
        I'm pretty sure the issue is not QDir::toNativeSeparators.

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

        1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @Puppy-Bear if you're using QFile, than there is no need for toNativeSeparators QFile operates on normal forwards slashes and handles those automatically correctly, when talking to system level functions/apis

          Puppy BearP Offline
          Puppy BearP Offline
          Puppy Bear
          wrote on last edited by Puppy Bear
          #4

          @J-Hilk Thanks for the help,
          I've searched and think i'm supposed to use '/' in windows,
          but is it possible to make different separators converted to the right one?
          espically when the path is given by a user?

          J.HilkJ 1 Reply Last reply
          0
          • Puppy BearP Puppy Bear

            @J-Hilk Thanks for the help,
            I've searched and think i'm supposed to use '/' in windows,
            but is it possible to make different separators converted to the right one?
            espically when the path is given by a user?

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @Puppy-Bear said in QFile path's toNativeSeparators returns wrong value on separator:

            @J-Hilk Thanks for the help,
            I've searched and think i'm supposed to use '/' in windows,
            but is it possible to make different separators converted to the right one?
            espically when the path is given by a user?

            no, not when you're using Qt!
            QFile is able to work with both, no problem at all.

            
            int main(int argc, char *argv[])
            {
                QGuiApplication app(argc, argv);
            
                QFile fileFSlash("C:/Qt/Tools/QtCreator/bin/qtcreator.exe");
                QFile fileBSlash("C:\\Qt\\Tools\\QtCreator\\bin\\qtcreator.exe");
            
                qDebug() << fileFSlash.exists() << fileBSlash.exists(); //results in true and true
            
            }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            3
            • kkoehneK Offline
              kkoehneK Offline
              kkoehne
              Moderators
              wrote on last edited by kkoehne
              #6

              @Puppy-Bear said in QFile path's toNativeSeparators returns wrong value on separator:

              but is it possible to make different separators converted to the right one?

              @J-Hilk is right that you don't have to convert to forward slashes. But you can use QDir::fromNativeSeparators() to normalize to forward slashes on all platforms.

              Or go through QFileInfo - QFileInfo("C:\\dev") .filePath() will also return "C:/dev".

              Director R&D, The Qt Company

              1 Reply Last reply
              0
              • Puppy BearP Puppy Bear

                HI everyone
                I'm trying to set a path with this function
                void Save_DB::set_new_path(QString path)
                {
                new_path = QDir::toNativeSeparators(path);
                if (!QFile(new_path).exists())
                qDebug() << "file not exist!\n";
                return;
                }
                and try to examine it with my own file.
                I'm pretty sure that there's something wrong with tonativeSeparators

                my input is
                D:/XL_db.db
                and the QFile.exists() returns false
                I've printed it which shows
                D:XL_db.db

                any idea of convertion of different separators like '' '//' '/' '\' in win10 msvc2015 qt4.9?

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #7

                @Puppy-Bear said in QFile path's toNativeSeparators returns wrong value on separator:

                any idea of convertion of different separators like '' '//' '/' '' in win10 msvc2015 qt4.9?

                First, please note that Qt internally always uses / as path separator, event when running on Windows.
                BUT you can use QDir::toNativeSeparators() to convert a path to use the native notation.
                This can be useful when you want to use the path with an external application.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                0

                • Login

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