Results 1 to 11 of 11

Thread: Image not appearing in Qlabel after compiling

  1. #1
    Join Date
    Oct 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4

    Default 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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4

    Default 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...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Image not appearing in Qlabel after compiling

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4

    Default 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?

  6. #6
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. #include <QPixmap>
    2. QPixmap pix("/home/your_name/picture_of_my_cat_093495.jpg");
    3. ui->label->setPixmap(pix);
    To copy to clipboard, switch view to plain text mode 

    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.

  7. The following user says thank you to boudie for this useful post:

    cheyanne (10th October 2011)

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Image not appearing in Qlabel after compiling

    Quote Originally Posted by cheyanne View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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.

  10. #9
    Join Date
    Oct 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4

    Default 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.

  11. #10
    Join Date
    Oct 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  12. #11
    Join Date
    Oct 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4

    Default Re: Image not appearing in Qlabel after compiling

    Quote Originally Posted by moorepe View Post
    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

Similar Threads

  1. Image in QLabel not being displayed
    By P@u1 in forum Qt Programming
    Replies: 2
    Last Post: 4th July 2011, 10:47
  2. how to move the image in QLabel
    By augusbas in forum Qt Programming
    Replies: 1
    Last Post: 13th October 2010, 09:33
  3. Replies: 6
    Last Post: 21st September 2009, 11:55
  4. QLabel as an image.
    By RSX in forum Newbie
    Replies: 2
    Last Post: 4th April 2009, 20:22
  5. Image on QLabel and resizeEvent
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 12th September 2008, 10:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.