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. Path/Directory separator inconsistent.
Forum Updated to NodeBB v4.3 + New Features

Path/Directory separator inconsistent.

Scheduled Pinned Locked Moved Unsolved General and Desktop
26 Posts 5 Posters 4.2k 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.
  • SPlattenS SPlatten

    @J-Hilk , why do I need to call another function in order to correct it? Surely the intuitive thing would be for the separator just to return the correct separator or test the content and return whatever is already being used.

    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by JonB
    #9

    @SPlatten said in Path/Directory separator inconsistent.:

    Surely the intuitive thing would be for the separator

    Because then Qt paths, like C:/Users/simon/Documents/ProjectName/build-ProjectName-Desktop_Qt_5_8_0_MSVC2013_32bit-Debug, would never return a separator if you searched them for it.

    As others have said:

    • QDir::separator() will always be / platform-independent, and as used in all internal Qt functions.

    • QDir::toNativeSeparators(QDir::separator()) will return \ under Windows. Which you only need if you're doing something to do with OS commands. What else do you actually need it for?

    Why does this irk you so, seems fine to me?

    1 Reply Last reply
    0
    • SPlattenS SPlatten

      @J-Hilk , why do I need to call another function in order to correct it? Surely the intuitive thing would be for the separator just to return the correct separator or test the content and return whatever is already being used.

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

      @SPlatten said in Path/Directory separator inconsistent.:

      @J-Hilk , why do I need to call another function in order to correct it? Surely the intuitive thing would be for the separator just to return the correct separator or test the content and return whatever is already being used.

      Nono, you're comparing Apple and Oranges!

      QDir::currentPath().toLatin1().data()

      returns a Qt conform string with '/' as separators than you check with

      QChar separator = QDir::separator();

      if '\' is the ending that can't work.

      After seeing this I then appended a separator on the path before the test just to see what happens.

      this is still confusing, can you show what exactly you'Re doing here ?


      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.

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

        @SPlatten said in Path/Directory separator inconsistent.:

        @J-Hilk , why do I need to call another function in order to correct it? Surely the intuitive thing would be for the separator just to return the correct separator or test the content and return whatever is already being used.

        Nono, you're comparing Apple and Oranges!

        QDir::currentPath().toLatin1().data()

        returns a Qt conform string with '/' as separators than you check with

        QChar separator = QDir::separator();

        if '\' is the ending that can't work.

        After seeing this I then appended a separator on the path before the test just to see what happens.

        this is still confusing, can you show what exactly you'Re doing here ?

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #11

        @J-Hilk , this was the code:

        QChar separator = QDir::separator();
        QString newPath(path);
        
        if (!newPath.endsWith(separator)) {
            newPath += separator;
        }
        

        @JonB, you can't pass QDir::separator() to QDir::toNativeSeparators as there is only one version of this function and it expects a QString not a QChar.

        It's all just messy and requires thought and effort when it should be handled internally.

        Kind Regards,
        Sy

        J.HilkJ KroMignonK JonBJ 3 Replies Last reply
        -1
        • SPlattenS SPlatten

          @J-Hilk , this was the code:

          QChar separator = QDir::separator();
          QString newPath(path);
          
          if (!newPath.endsWith(separator)) {
              newPath += separator;
          }
          

          @JonB, you can't pass QDir::separator() to QDir::toNativeSeparators as there is only one version of this function and it expects a QString not a QChar.

          It's all just messy and requires thought and effort when it should be handled internally.

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

          @SPlatten said in Path/Directory separator inconsistent.:

          It's all just messy and requires thought and effort when it should be handled internally.

          it is handled internally, thats the point. The only possibly confusing point could be, that QDir has no QDir::QtSeperator() that returns a QChar('/'); that you could use.

          Simply write

          QChar separator('/');
          if (!path.endsWith(separator) {
              path += separator;
          }
          

          and use toNativeSeperators, if you actually need the native part, for example to display the path or to hand it of to an external non qt program


          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
          1
          • SPlattenS SPlatten

            @J-Hilk , this was the code:

            QChar separator = QDir::separator();
            QString newPath(path);
            
            if (!newPath.endsWith(separator)) {
                newPath += separator;
            }
            

            @JonB, you can't pass QDir::separator() to QDir::toNativeSeparators as there is only one version of this function and it expects a QString not a QChar.

            It's all just messy and requires thought and effort when it should be handled internally.

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

            @SPlatten said in Path/Directory separator inconsistent.:

            It's all just messy and requires thought and effort when it should be handled internally.

            There is no mess here, it is clearly documented:

            • QDir always use '/' as path separator. So path string can the same on every system for which your application is build
            • QDir::separator() gives you the system path separator on which application is running
            • QDir::toNativeSeparators(), will convert the given string to a string using QDir::separator() so it will be acceptable by the system.
            • QDir:cleanPath(), will ensure you path has no redondant separators, and use '/' as separator

            So your code is wrong.
            This could work:

            QString newPath(QDir::cleanPath(path));
            
            if (!newPath.endsWith('/')) {
                newPath += '/';
            }
            

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

            SPlattenS 1 Reply Last reply
            2
            • KroMignonK KroMignon

              @SPlatten said in Path/Directory separator inconsistent.:

              It's all just messy and requires thought and effort when it should be handled internally.

              There is no mess here, it is clearly documented:

              • QDir always use '/' as path separator. So path string can the same on every system for which your application is build
              • QDir::separator() gives you the system path separator on which application is running
              • QDir::toNativeSeparators(), will convert the given string to a string using QDir::separator() so it will be acceptable by the system.
              • QDir:cleanPath(), will ensure you path has no redondant separators, and use '/' as separator

              So your code is wrong.
              This could work:

              QString newPath(QDir::cleanPath(path));
              
              if (!newPath.endsWith('/')) {
                  newPath += '/';
              }
              
              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #14

              @KroMignon , I'm writing an application that uses paths that may be supplied in text files, the application should not rely on what the Qt library uses.

              Kind Regards,
              Sy

              KroMignonK 1 Reply Last reply
              0
              • SPlattenS SPlatten

                @KroMignon , I'm writing an application that uses paths that may be supplied in text files, the application should not rely on what the Qt library uses.

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

                @SPlatten said in Path/Directory separator inconsistent.:

                I'm writing an application that uses paths that may be supplied in text files, the application should not rely on what the Qt library uses.

                And what is the problem with that?
                Simply use QDir:cleanPath() to ensure your path string is correct.

                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
                1
                • SPlattenS SPlatten

                  @J-Hilk , this was the code:

                  QChar separator = QDir::separator();
                  QString newPath(path);
                  
                  if (!newPath.endsWith(separator)) {
                      newPath += separator;
                  }
                  

                  @JonB, you can't pass QDir::separator() to QDir::toNativeSeparators as there is only one version of this function and it expects a QString not a QChar.

                  It's all just messy and requires thought and effort when it should be handled internally.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #16

                  @SPlatten said in Path/Directory separator inconsistent.:

                  @JonB, you can't pass QDir::separator() to QDir::toNativeSeparators as there is only one version of this function and it expects a QString not a QChar.

                  Why do you make this kind of statement without checking?

                      QString sep = QDir::toNativeSeparators(QDir::separator());
                  
                  
                  1 Reply Last reply
                  0
                  • SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #17

                    I did exactly that, in the version of Qt Creator I'm using there is only one function.

                    Kind Regards,
                    Sy

                    JonBJ 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      I did exactly that, in the version of Qt Creator I'm using there is only one function.

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #18

                      @SPlatten
                      No, you did not try the code! There is just the one function in my version of Qt too! And it accepts the same parameter type as yours.... static QString toNativeSeparators(const QString &pathName);, https://doc.qt.io/qt-5/qdir.html#toNativeSeparators.

                      SPlattenS 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @SPlatten
                        No, you did not try the code! There is just the one function in my version of Qt too! And it accepts the same parameter type as yours.... static QString toNativeSeparators(const QString &pathName);, https://doc.qt.io/qt-5/qdir.html#toNativeSeparators.

                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #19

                        @JonB , Qt Creator 4.2.1, Based on Qt 5.8.0 (MSCV 2015, 32 bit), thats the version on the laptop I'm using for this project and I can sure you that the version only excepts a const QString&, there is no alternative in this IDE.

                        Kind Regards,
                        Sy

                        JonBJ 1 Reply Last reply
                        0
                        • SPlattenS SPlatten

                          @JonB , Qt Creator 4.2.1, Based on Qt 5.8.0 (MSCV 2015, 32 bit), thats the version on the laptop I'm using for this project and I can sure you that the version only excepts a const QString&, there is no alternative in this IDE.

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by JonB
                          #20

                          @SPlatten
                          It has nothing to do with the IDE, at all.

                          I can sure you

                          It does have to do with the behaviour of C++, and QString....

                          Would you be kind enough to copy & paste my line into your version, and tell me the outcome, please? Show me the error message you get at compile-time, thank you.

                          1 Reply Last reply
                          0
                          • SPlattenS Offline
                            SPlattenS Offline
                            SPlatten
                            wrote on last edited by
                            #21

                            @JonB , QDir::separator does not return a QString it returns a QChar.

                            Kind Regards,
                            Sy

                            JonBJ 1 Reply Last reply
                            -1
                            • SPlattenS SPlatten

                              @JonB , QDir::separator does not return a QString it returns a QChar.

                              JonBJ Online
                              JonBJ Online
                              JonB
                              wrote on last edited by
                              #22

                              @SPlatten
                              I KNOW THIS.

                              HAVE YOU ACTUALLY TRIED IT, DESPITE WHAT YOU SAY??????????

                              SPlattenS 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @SPlatten
                                I KNOW THIS.

                                HAVE YOU ACTUALLY TRIED IT, DESPITE WHAT YOU SAY??????????

                                SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by
                                #23

                                @JonB How can I try it when it won't compile without errors? Stop shouting and calm down!

                                Kind Regards,
                                Sy

                                JonBJ jsulmJ 2 Replies Last reply
                                -1
                                • SPlattenS SPlatten

                                  @JonB How can I try it when it won't compile without errors? Stop shouting and calm down!

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on last edited by
                                  #24

                                  @SPlatten
                                  Could you please put it into your code, and show me the compile-time error message you get, as requested? I should be most obliged....

                                  SPlattenS 1 Reply Last reply
                                  0
                                  • SPlattenS SPlatten

                                    @JonB How can I try it when it won't compile without errors? Stop shouting and calm down!

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

                                    @SPlatten Sorry, but I have to support @JonB here: that line of code compiles just fine.
                                    "it returns a QChar" - which converts just fine to a QString (https://doc.qt.io/qt-5/qstring.html#QString-2).

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

                                    1 Reply Last reply
                                    1
                                    • JonBJ JonB

                                      @SPlatten
                                      Could you please put it into your code, and show me the compile-time error message you get, as requested? I should be most obliged....

                                      SPlattenS Offline
                                      SPlattenS Offline
                                      SPlatten
                                      wrote on last edited by
                                      #26

                                      @JonB , sorry, I just don't have time to back tract now, apologies if I was wrong.

                                      Kind Regards,
                                      Sy

                                      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