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. Relativ path
Qt 6.11 is out! See what's new in the release blog

Relativ path

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 449 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.
  • C Offline
    C Offline
    Cervo2paille
    wrote on last edited by
    #1

    Hello,
    I'm trying to get a path in my project : the repository "images". + a name of file (that i want to copy).
    Works with absolute path, but not relative :
    Works :

                QFile::copy(nomFichier, "D:/Users/flebourgeois/Desktop/blossom/images/"+lastBit);
    

    Doesn't works :

                QFile::copy(nomFichier, ":/images/"+lastBit);
    or
                QFile::copy(nomFichier, "/images/"+lastBit);
    

    Any idea?
    Thanks.

    JonBJ 1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      ":/images/" is an embedded resource path, so no, it won't work with files in your filesystem.
      "/images/" is an absolute path.

      What do you want your path to be relative to?

      1 Reply Last reply
      2
      • C Cervo2paille

        Hello,
        I'm trying to get a path in my project : the repository "images". + a name of file (that i want to copy).
        Works with absolute path, but not relative :
        Works :

                    QFile::copy(nomFichier, "D:/Users/flebourgeois/Desktop/blossom/images/"+lastBit);
        

        Doesn't works :

                    QFile::copy(nomFichier, ":/images/"+lastBit);
        or
                    QFile::copy(nomFichier, "/images/"+lastBit);
        

        Any idea?
        Thanks.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Cervo2paille said in Relativ path:

        Doesn't works :

        ":/images/"+lastBit: This is a path for a Qt resource, for when you embed the image into your executable. If you are using an external file it is not suitable.

        "/images/"+lastBit: This is an absolute path, rooted at \image on the current drive. Your file is (presumably) not there.

        "D:/Users/flebourgeois/Desktop/blossom/images/"+lastBit: This is the "correct" way to specify a path.

        A relative path would be images/.... But at runtime that would be relative to the current working directory, and since you do not know what that would be it is quite unsuitable to use to locate an existing file.

        Your "project" or "project directory" does not exist at runtime. It only exists at development time.

        You have two choices to locate your file at runtime:

        • Place it somewhere relative to where your executable is, and use QCoreApplication::applicationDirPath() to locate it.

        • Place it in a suitable directory among those documented in QStandardPaths.

        1 Reply Last reply
        2
        • C Offline
          C Offline
          Cervo2paille
          wrote on last edited by
          #4

          Thanks for the explains.
          I used :

              QString currentPath = QDir::currentPath()+"/tmp_images/";
              qInfo() << "currentPath: "+currentPath;
              QDir dir(currentPath);
              if (!dir.exists()){
                qDebug() << "Creating " << currentPath << "directory";
                dir.mkpath(currentPath);
              }
          

          And it's works !

          JonBJ 1 Reply Last reply
          0
          • C Cervo2paille

            Thanks for the explains.
            I used :

                QString currentPath = QDir::currentPath()+"/tmp_images/";
                qInfo() << "currentPath: "+currentPath;
                QDir dir(currentPath);
                if (!dir.exists()){
                  qDebug() << "Creating " << currentPath << "directory";
                  dir.mkpath(currentPath);
                }
            

            And it's works !

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Cervo2paille
            Your original question was:

            I'm trying to get a path in my project : the repository "images". + a name of file (that i want to copy).

            You are now showing code which picks a temporary directory to write images to, which is quite different.

            QDir::currentPath() just returns the current working directory. QDir::currentPath()+"/tmp_images/" and "tmp_images/" will refer to the same path. You have not taken on board the point about this being unreliable, your code may not work as you expect if you give it to someone else or try it outside Creator.

            1 Reply Last reply
            2

            • Login

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