Image not appearing in Qlabel after compiling
Hi, I'm currently trying to display an image in Qt form using the label because I have search online that using label is an easy way to do it. So in my qt window, under the Qlabel tab, i choose a file from pixmap from my directories. When i preview it, it's working fine but when i compile my code and run, everything show up except for the image.
I'm using Qt4 and running on linux platform.
Re: Image not appearing in Qlabel after compiling
You are probably using a relative path to the image. Use an absolute one or one relative to a well defined directory.
Re: Image not appearing in Qlabel after compiling
Sorry but i dont really get what you mean, because when i choose the file from qt design itself the image can be shown. I just dont know why the image disappear when i compile program and run it...
Re: Image not appearing in Qlabel after compiling
Re: Image not appearing in Qlabel after compiling
I got it. But I'm sure my path to the image is right. Is there anything I need to add to my codes that I've forgotten?
Re: Image not appearing in Qlabel after compiling
I guess you set the filename for the pixmap in QtCreator?
If yes, then try to set it in the constructor of your mainwindow:
Code:
#include <QPixmap>
QPixmap pix
("/home/your_name/picture_of_my_cat_093495.jpg");
ui->label->setPixmap(pix);
If it's still not working, then make a subdirectory under the directory where your executable is (note that it might not be the directory where your source code is).
Name that new directory "imageformats" and copy libqjpeg.so from your qt directory "qt/plugins/imageformats". You will find more plugins there. Copy whatever you need.
Re: Image not appearing in Qlabel after compiling
Quote:
Originally Posted by
cheyanne
But I'm sure my path to the image is right.
No, you only think it is right. If your have a file /a/b/c.png and your current working directory is /a then using the path of b/c.png will find the file. However if you current working directory is /x/y/z then using the path b/c.png will not find that file. Designer uses a different current working directory than when your program is compiled. Check the code generated by uic to find what path gets encoded into the binary.
Re: Image not appearing in Qlabel after compiling
An alternative way is to use the resources, so the image added (into the application) as a resource and than use that "resource-path" - this way you don't have to know the image path, because the image is inside your application - see this link for more details.
Re: Image not appearing in Qlabel after compiling
Another question, if I wan to get my robot footprint to appear on the map that I have inserted to the qt how do I do it. I wan it to be able to move according to where my robot moves. There is another server laptop on the robot itself, the qt is the client side.
Re: Image not appearing in Qlabel after compiling
I just sorted this out the other day. What has happened is that the location of the image is not the same relative path when you set it in QtCreator's designer which uses the source dir. You may have something like image/thing.png in source dir, but since the executable is in the build dir, you would need a copy of the image/thing.png structure in the debug or release directory to match your source directory.
The best thing to do is to add the image as a resource which is linked into the executable, then select this resource using a path ":image/thing.png" instead of selecting a file. This is a good link to describe how to set a resource http://www.youtube.com/watch?v=sWIQIi4lg58
Then you highlight your Qlabel and select your pixmap resource (not file) using the dropdown on the pixmap property.
Re: Image not appearing in Qlabel after compiling
Quote:
Originally Posted by
moorepe
I just sorted this out the other day. What has happened is that the location of the image is not the same relative path when you set it in QtCreator's designer which uses the source dir. You may have something like image/thing.png in source dir, but since the executable is in the build dir, you would need a copy of the image/thing.png structure in the debug or release directory to match your source directory.
The best thing to do is to add the image as a resource which is linked into the executable, then select this resource using a path ":image/thing.png" instead of selecting a file. This is a good link to describe how to set a resource
http://www.youtube.com/watch?v=sWIQIi4lg58
Then you highlight your Qlabel and select your pixmap resource (not file) using the dropdown on the pixmap property.
Thanks for replying. I've already succeeded in adding display my image but noe my another concern is about how to draw a circle/rectangle on top of the image. I've created another thread posting the question.http://www.qtcentre.org/threads/4514...r-Qlabel-image