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. Can Qt build "paths" ?
QtWS25 Last Chance

Can Qt build "paths" ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qdir
4 Posts 3 Posters 781 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.
  • D Offline
    D Offline
    Dariusz
    wrote on 17 Jan 2023, 20:25 last edited by
    #1

    Hey

    I'm in need of some "path building" that is correct depending on platform.

    Would qt be able to provide it ? something similar to python Path().joinpath("this", "is", "my", "fancy", "path");

    ?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JoeCFD
      wrote on 17 Jan 2023, 20:37 last edited by JoeCFD
      #2

      Check QDir out

      QStringList nameList;
      // add names to nameList
      QDir dir(nameList.join(QDir::separator())); /* no worries*/

      D 1 Reply Last reply 17 Jan 2023, 21:31
      1
      • J JoeCFD
        17 Jan 2023, 20:37

        Check QDir out

        QStringList nameList;
        // add names to nameList
        QDir dir(nameList.join(QDir::separator())); /* no worries*/

        D Offline
        D Offline
        Dariusz
        wrote on 17 Jan 2023, 21:31 last edited by
        #3

        @JoeCFD Yea I know I can kinda hack it, but windows want \ mac / and so on... same with \192.168.10.10\some\network\path etc etc...

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 17 Jan 2023, 21:40 last edited by
          #4

          Internally Qt uses / as path separator, but all path taking APIs accept both / and \, so you can just always use / (which is easier because you don't have to escape it).
          If you need to pass the path to some native platform API or display it to the user in a platform correct form you can use QDir::toNativeSeparators, which will convert all the separators to current platform appropriate.
          It's usually not needed, but if you insist on building the platform correct path yourself you can use QDir::separator().

          So both of those will do the same:

          QString path1 = QDir::toNativeSeparators("\\this/is\\my/fancy\\path");
          
          QChar sep = QDir::separator();
          QString path2 = sep + "this" + sep + "is" + sep + "my" + sep + "fancy" + sep + "path".
          
          1 Reply Last reply
          3

          2/4

          17 Jan 2023, 20:37

          • Login

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