Couldn't loading .png or .jep picture,by qt create Running Executable
-
/picture on exe:/
https://ddgobkiprc33d.cloudfront.net/952ef1f4-b4de-4456-9896-4214dd7d062e.png
Loading fail!/*No thing in here */
/On the .ui output/
picture loading successfully.
/*Picture on the source coding */
https://ddgobkiprc33d.cloudfront.net/84349671-5286-4288-8554-d42f09d3c9ed.pngLoading successfully in .cpp sources code.
Couldn't output on executable pictures maybe is qt cause or my
Ubuntu I think.i need help,find the cause , after l can loading any
picture on qt creator running exe. -
Since the image path you're using is relative, it behaves differently depending on where the program is run from. When running from Qt Creator, the working directory is usually the project root, so a path like images/background.png works if the images folder is in your project directory.
However, when you build and run the app outside of Qt Creator, the working directory becomes the folder where the executable is located (e.g., a build folder or wherever you've placed the compiled app). In that case, the relative path images/background.png might not be valid anymore.
You have a couple of options to fix this:
- Copy the image folder to the same directory as your executable.
- Use a Qt resource file (.qrc) to embed the image directly into your application.
-
Is not .qrc path problems , i think my exe don't understand what is the picture?,so l should be write some codes,let my exe know , what is the picture nya~~(◍•ᴗ•◍)
@Maybecute the exe can read and load .txt .log
.cxx, only read text information , If it can loading about picture example " .png ", " . jep "and more . ,my problem maybe solved. -
@Kevin-Hoang is right. It's a path problem. Use QFile::exists() so you can see that he is correct.
-
Since the image path you're using is relative, it behaves differently depending on where the program is run from. When running from Qt Creator, the working directory is usually the project root, so a path like images/background.png works if the images folder is in your project directory.
However, when you build and run the app outside of Qt Creator, the working directory becomes the folder where the executable is located (e.g., a build folder or wherever you've placed the compiled app). In that case, the relative path images/background.png might not be valid anymore.
You have a couple of options to fix this:
- Copy the image folder to the same directory as your executable.
- Use a Qt resource file (.qrc) to embed the image directly into your application.
@Kevin-Hoang said in Couldn't loading .png or .jep picture,by qt create Running Executable:
However, when you build and run the app outside of Qt Creator, the working directory becomes the folder where the executable is located (e.g., a build folder or wherever you've placed the compiled app).
No it does not. Running an executable does not change the working directory in any way, and not to where the executable is located. Working directory is inherited (or set) from caller. It is possible that OS/windowing system allows user to specify a current directory e.g. on an icon/shortcut to run it, but that is another matter. Not to mention that an executable can change the current directory itself any time, e.g. before trying to access the relative path. All of which makes it even more important that coder think about how they specify paths or what directories to use.
This means that your "Copy the image folder to the same directory as your executable." is not at all a guarantee it will be found via a relative path. Don't use relative paths unless you really mean "relative to whatever the current directory happens to be", which is not suitable for locating supplied files.
P.S.
If you really want to locate files relative to where the executable lives use QCoreApplication::applicationDirPath() in code to build an absolute path. Even then note this is not robust, at minimum under Linux. -
Find the reason at https://forum.qt.io/topic/71836/how-to-display-a-picture/17
Now is successful load pictures on application,
Picture loading path couldn't is .qrc , should be
/Home add new Folders to save of picture .
and add some main code at transformat::transformat/* note: picture path could not is /test_background/picture name.jpg It's is .qrc loading path,not applicable output Picture path. */ Qpixmap setpicture{”home/background pictures/picture name.jpg"}; ui->setpic->setPixmap(setpicrure); ui->setpic->setScaledContent(true);
-
M Maybecute has marked this topic as solved
-
@Kevin-Hoang said in Couldn't loading .png or .jep picture,by qt create Running Executable:
However, when you build and run the app outside of Qt Creator, the working directory becomes the folder where the executable is located (e.g., a build folder or wherever you've placed the compiled app).
No it does not. Running an executable does not change the working directory in any way, and not to where the executable is located. Working directory is inherited (or set) from caller. It is possible that OS/windowing system allows user to specify a current directory e.g. on an icon/shortcut to run it, but that is another matter. Not to mention that an executable can change the current directory itself any time, e.g. before trying to access the relative path. All of which makes it even more important that coder think about how they specify paths or what directories to use.
This means that your "Copy the image folder to the same directory as your executable." is not at all a guarantee it will be found via a relative path. Don't use relative paths unless you really mean "relative to whatever the current directory happens to be", which is not suitable for locating supplied files.
P.S.
If you really want to locate files relative to where the executable lives use QCoreApplication::applicationDirPath() in code to build an absolute path. Even then note this is not robust, at minimum under Linux.