How to display image in directory?
-
wrote on 8 Feb 2013, 01:46 last edited by
Hi I am quite new to qt. I wrote a program which display image in label. But at first I put image in build folder of my program. It can display fine. But when I move my image to home folder, it cannot display. My image is at Home folder. I am using ubuntu 11.10. my program is like that
@
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QLabel>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QImage myImage;
myImage.load(":/Home/1.jpeg");QLabel myLabel; myLabel.setPixmap(QPixmap::fromImage(myImage)); myLabel.show(); return a.exec();
}
@If anything wrong, pls kindly tell me. Thanks.
Edit: please enclose code snippets in @ tags; Andre
-
wrote on 8 Feb 2013, 04:39 last edited by
Maybe change
@myImage.load(”:/Home/1.jpeg”);@to
@myImage.load(”/home/1.jpeg”);@
-
wrote on 8 Feb 2013, 05:50 last edited by
still cannot :(
[quote author="qxoz" date="1360298371"]Maybe change
@myImage.load(”:/Home/1.jpeg”);@to
@myImage.load(”/home/1.jpeg”);@[/quote]
-
wrote on 8 Feb 2013, 06:02 last edited by
Sorry i was wrong.
In ubuntu as i know home path is /home/username/
So check the real path of file, it must be something like /home/username/1.jpeg -
wrote on 8 Feb 2013, 06:28 last edited by
but my image is at home folder. Do i need to put username as well?
-
wrote on 8 Feb 2013, 07:29 last edited by
Still cannot :(
-
wrote on 8 Feb 2013, 07:32 last edited by
now i can get it as you said. thanks a lot :)
[quote author="qxoz" date="1360303353"]Sorry i was wrong.
In ubuntu as i know home path is /home/username/
So check the real path of file, it must be something like /home/username/1.jpeg[/quote] -
wrote on 8 Feb 2013, 11:13 last edited by
Hello
I know this was solved, but you could avoid future problems with images when using "Qt's resources mechanism":http://qt-project.org/doc/qt-4.8/resources.html.
bq. The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable. This is useful if your application always needs a certain set of files (icons, translation files, etc.) and you don't want to run the risk of losing the files.
Hope this helps a little bit more and takes you one step further :)
Regards
1/8