How to display image in directory?



  • 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(&#41;;
    

    }
    @

    If anything wrong, pls kindly tell me. Thanks.

    Edit: please enclose code snippets in @ tags; Andre



  • Maybe change
    @myImage.load(”:/Home/1.jpeg”);@

    to

    @myImage.load(”/home/1.jpeg”);@



  • still cannot :(
    [quote author="qxoz" date="1360298371"]Maybe change
    @myImage.load(”:/Home/1.jpeg”);@

    to

    @myImage.load(”/home/1.jpeg”);@[/quote]



  • 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



  • but my image is at home folder. Do i need to put username as well?



  • Still cannot :(



  • 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]



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.