Relativ path
-
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. -
":/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?
-
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.@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\imageon 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.
-
-
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 !
-
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 !
@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.