Can't load Image source in QML Image - image

This seems really basic but for some reason I can't get the image source to work in the ultra-simple QML app below.
FYI, I'm running Mac OS-X 10.9.5, Qt Creator 3.2.1 based on Qt 5.3.2.
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Test")
Image {
id: image1
x: 10
y: 10
width: 100
height: 100
source: "testImage.png"
}
}
I get the following error:
qrc:/main.qml:10:5: QML Image: Cannot open: qrc:/testImage.png
The application window is created but no image is displayed. I've also tried wrapping the image within a Rectangle but this doesn't help.
What am I doing wrong here?
The "testImage.png" is in the project directory and I've tried all sorts of ways to specify the image path using resources, absolute, relative, and even specifying the image source manually with the QML UI designer.
I'll also mention that to get Qt 5.3.2 to work I followed the modification suggested HERE.
Thanks.

I suppose that your qml file is in resources, so try place your testImage.png image in resources too. Recompile app, run it and check is image works properly.
All should be together. For example:
My code:
Image
{
source: "images/earth.png"
If I delete this image from resources, I'll get this error too, but image still in the correct place in file system.
When qml source placed in the resource, then all files, which loads from qml should be in resource too (Qt searchs this files in the resources, not in the file system).

If you are locating from local filesystem , get the file path and prefix "file:" as source = "file:/root/Desktop/Rathod.png"

If you are using qrc, make sure the image is added to your project. Just having it in your source directory may not be enough.
For example, my main.qml is located in a particular directory. I move my .png into the same directory. I then "Add Existing Files.." and add the .png to the project. My image has the following
source:"qrc:<same dir as main.qml>/image.png"

I had a similiar problem and solved it the following way:
Right click on your qml.qrc, then choose "open with" -> "Resource
Editor"
Once you are in the Resource Editor click on "Add missing file", there choose your image
Lastly open your qml file in the Design editor and click on the Image element, on the right side choose the path to the image

There can be two problems:
The image is corrupt, although the image was correctly displayed in all previews IconImage refuses to load it. Try to use another image that is known as work correctly
It seems that the Qt Image loader is very sensitive to the image format.
There is a resource cache problem. Try to delete qml_qmlcachea.qrc in your compiling folder and try it again

Try to close Shadow Build under Projects->Build Settings.

Related

QML images are not loaded, but everything works in the QT Designer

I have a problem.
Set: QT Creator 9.01 and QT 6.4
I'm creating an empty project Quick QT Application
I use the Build System - Cmake
The project has only a Window with dimensions and an image as a background.
Here is my Code:
import QtQuick 2.6
Window {
width: 375
height: 812
visible: true
color: "#28333f"
title: qsTr("Hello World")
Image {
id: background
anchors.fill: parent
source: "images/background.svg"
fillMode: Image.PreserveAspectFit
}
}
When I launch the application I get an error
QML Image: Unable to open: qrc:/background.svg
But at the same time, if I open this project in designer mode, I see that the image is inserted correctly, it is present and there are no errors.
Here is a screenshot.
enter image description here
After Googling and reading the forums, I created a res resource file.qrc to which I added the prefix / and add image file.
In the code, I change the path to the image file to this:
source: "/images/background.svg"
^^
And then when I start the project, everything works correctly for me and an image appears.
But another problem arises.
If you switch to Designer mode. All images disappear
How to win it ?

How to load images (jpg) in QML generated at runtime

my question is quite simple.
I have a Qt application with a Gui developed with QML.
My back-end side generates and saves jpg images in a folder called /images .
I am able to send from C++ to QML the strings holding the name of the images that I want to show. Sadly my QML Image object doesn't display anything.
It seems that if an image can be displayed only if it has been previously declared in the qml.qrc file.
Sadly, again, qrc files do not support wildcards yet!
https://bugreports.qt.io/browse/QTBUG-1400
Is there a workaround?
The name of the images are not fixed, they are generated with certain runtime values i.e. timestamp.
Thank you
By default it will search the image source in the qrc file. To make it go to the actual file system, prepend a file:/// to the path.

Impossible to reference an image from the Assets Folder

I am currently building a Windows Universal App and the learning curve is OK. I get a response from my webservice, parse the JSON, display info in the app, but my current issue is the following one:
I tried to display an image that I put in the Assets folder, but this doesn't seem to work.
This is my layout:
As you can see, the text "Test" is correctly displayed but not the images.
On the right side, in the explorer pannel, I cannot see the files that should be here:
WHAT I HAVE TRIED:
In my example, they are in the Assets/picto folder, but I have also tried the root directory Assets, as wal as creating Image folder at the same level.
I have definitely no idea how to add a simple picture, so any help is appreciated!
Adding items in Windows explorer to the right location doesn't add them to your solution / deployment.
You have to right-click the picto folder -> Add > -> Add existing item. Your items are not even in your solution, so they won't show up on the layout screen.
Make sure you set the correct build target for each image.
If an image shouldn't show up, even through it's in your solution, this is often the reason.

Interface Builder doesn't show some of my resource images

I have almost 20 images in PNG format added to my current project on Xcode (under the proper group, Resources). Unfortunately, library window's Media tab doesn't show some of them. And if I create an UIImageView and set one of these invisible images from code, nothing shows up. I also tried from IB with adding a UIImageView and set its source image.
Is this problem a image format related issue or is there a tiny detail I'm missing.
What log says:
Could not load the "07_clock_icon.png" image referenced from a nib in the bundle
The solution to this is a bit silly, but it definitely works:
Close XCode and the Interface Builder. Now, instead of opening XCode then opening the project, navigate to the project in Finder and double click on the project. When you open your xib file the blue file not found images will flash up, then be replaced with the correct images and the dropdown will show all your images in the Inspector.
Heyy! I figured it out. It's a refreshing bug. So just copy and replace those images that did not load into your project folder, and then hold Shift Command K (to clean your project). After Clean Up it should work perfectly! Enjoy!
If you dragged the images from the Finder into the Resources folder in XCode the images should show up in IB as well. At least it does so in my setup.
Maybe the images are 8-bit PNG images. I believe Cocoa requires them to be 24-bit because of the alpha transparency. PNG 8-bit doesn't support the alpha channel, as it's more like a GIF image.
But from the error you posted It seems IB couldn't find the resource at all. Which is understandable if it doesn't load the resources from XCode in its library.
Assuming you selected the option to let XCode copy the files into your project and neither XCode nor the Finder complain when you make the copy... I wonder if there might be some file corruption? Maybe try recreating them or opening and resaving them from another graphics application (or even Preview.app).
btw- 8-bit PNGs have worked fine for me.
Same problem was encountered by me. The problem is with the image. The image has been some how corrupted.Try to open the image in photoshop and you will see it doesn't open with photoshop.
I solved the problem by changing the image format to PNG-24 and everything works fine for me.
Hope this will solve your issue.
Regards,
Rupesh R Menon

How do I load an image using SDL and Xcode on OS X 10.5?

Edit: After using a bmp at toastie's suggestion, I'm still having problems loading the image:
I'm using SDL and OpenGL with Xcode and I'm trying to load an image to use as a texture on a cube. The image is a 256x256 RBG jpeg. The image is in the same directory as all of my source code, and it's under the Resources folder in the Xcode project. The file is named texture.bmp
if (textureSurface = SDL_LoadBMP("texture.bmp"))
{
// ...
}
else printf("%s", SDL_GetError());
I keep running it and getting the console error: Couldn't open texture.bmp
What is the path, or proper syntax for loading a file under these conditions?
SDL_LoadBMP only loads BMP files as its name would suggest :)
You will need another library to load other image formats.
Try SDL_image:
http://www.libsdl.org/projects/SDL_image/
or DevIL:
http://openil.sourceforge.net/
Or roll your own loader:
http://www.libpng.org/pub/png/libpng.html
Found the answer here. Essentially the image path is relative to the application being run, so I had to move the image or make the path relative to the debug build.
I don't have Xcode in front of me, but I think if you right/option click on the file in your resources listing to get at the preferences for the file you can set it to be relative to the project, containing directory, etc.

Resources