Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Display image on screen using qLabel



  • Hi,
    I want to display an image to screen, but It is not working. My code is:

    #include <QtGui/QApplication>
    #include <QLabel>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QPixmap pm("tiger.jpg");
    QLabel lbl;
    lbl.setPixmap(pm);
    lbl.showFullScreen();

    return a.exec();
    }

    Actually I have tried many ways to display, all were fail.
    Please let me know about it.

    Thanks
    Awadhesh



  • @Awadhesh-Maurya
    When you load an image external to the resources file, the image needs to be in the same folder than the executable. Do you have it placed there?


  • Moderators

    You should make sure your path is correct. Also, you should not base your path on current dir (like you are now), because it can change outside of your application (somebody can run the app from a different folder using the command line or a link). It is better to use QCoreApplication::applicationDirPath() as the base path.



  • @sierdzio
    I replace QPixmap pm("tiger.jpg"); with QPixmap pm("/root/images/tiger.jpg");
    but I am getting same, screen is white.
    tiger.jpg is 800x480 same as lcd used.


  • Moderators

    @Awadhesh-Maurya

    1. What version of Qt are you using?
    2. What platform are you using?


  • @JKSH
    Its 4.8.6 and linux-3.14



  • Hi,
    I am using Linux3.14 with buildroot system on Renesas RSKRZA1H board. I have build qt 4.8.6 with example and free license option in menuconfig.
    I have checked that any image is not displaying using qt in qt example also like icons example.

    Please let me know.


  • Moderators

    Hi @Awadhesh-Maurya,

    I haven't used Qt 4 in a long time. If I remember correctly, you need to make sure that the "image formats" shared libraries are present. For example,

    • /<dir>/MyExecutable
    • /<dir>/imageformats/qjpeg4.so

    Before that though, check that other simpler things are working correctly. If you create a QPushButton, does it get displayed?



  • Thank you JKSH.
    QPushButton is working and it's icon is just simple rounded rectangle.
    I qt example(imagescaling) if I select any image, error message is:

    QDesktopServices::storageLocation 6 not implemented
    QPixmap::scaleWidth: Pixmap is a null pixmap

    In icons example error:
    QPixmap::scaleWidth: Pixmap is a null pixmap

    Please let me know to solve these.


  • Lifetime Qt Champion

    Hi,

    Are you by any chance trying to load an image that is on your computer from your device ? If so, copy that image over to your device filesystem beside the application or to a place known to your application.



  • Actually JPEG and PNG packages were not selected in buildroot building. I have selected these and now these are working.

    Thanks all of you for your support.


Log in to reply