Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Images in qt application not loading.
QtWS25 Last Chance

[Solved] Images in qt application not loading.

Scheduled Pinned Locked Moved General and Desktop
24 Posts 6 Posters 28.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joshua-anderson
    wrote on last edited by
    #1

    Hello.

    My qt app seems to be missing something that I can't figure out. No matter how I try to set my qpushbutton icon to a png file (ui file , code) the button always remains blank. I have tried just plain loading it from file and using a qrc file. In the qt creator ui editor, the icon appears as it should when loading from both qrc and file, however when I try to run my app, nothing shows up. There is no error message in the log saying the icon cannot be found. I also have copied the image folder to the same directory as that the exe is in, just in case. I use this the code in my application to add my icon, but it doesn't do anything either. Is there a special item in the .pro file you need to have? Here is my setting the icon from script code ( I am including qicon and qpixmap).

    @QPixmap maximizePixmap(":/trackbox/icons/test.png"); //if i'm using reasources
    QPixmap maximizePixmap("icons/test.png"); //just loading from file
    QIcon maximizeButtonIcon(maximizePixmap);
    ui->pushButton->setIcon(maximizeButtonIcon);@

    My pro file looks like this:

    @QT += core gui svg xml widgets

    TARGET = Trackbox
    TEMPLATE = app

    SOURCES += main.cpp
    trackboxmainwindow.cpp

    HEADERS += trackboxmainwindow.h

    FORMS += trackboxmainwindow.ui

    RESOURCES +=
    trackbox.qrc@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Silly question but is it a custom built Qt with -no-png ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        joshua-anderson
        wrote on last edited by
        #3

        I am using qt 5.0.1 MVC 2012 from the qt website. While messing around with this project a while ago I was able to get the image to appear but when I tried again now, nothing I can do can make the icon appear. And this isn't a complex project, so there is only one window and one ui file, and I'm not doing anything wierd that I know of other than using a boarderless window. what really confuses me is that the icon shows up perfectly in qt creator, the app puts out no error messages in the debug log, but no images appear. The ui file is being loaded, any changes I make to it, like adding a button, works, even button text appears, but no icon.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Did you check if you had a null pixmap ? If it's the case you are probably using the wrong path from the qrc.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • J Offline
            J Offline
            joshua-anderson
            wrote on last edited by
            #5

            I am going to do that. Right now I an just loading from the normal filesystem in the ui file for absolute simplicity. If qt can't find the image file, it outputs a warning to the debug console I have noticed. Is this warning unreliable?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I am not sure that QPixmap prints a warning (I may be wrong though)

              It seems you're not using absolute path to your images on the filesystem. If you're using a shadow build, they won't be find.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • J Offline
                J Offline
                joshua-anderson
                wrote on last edited by
                #7

                What do you mean by shadow build? I also copied the image folder to the build directory where the built exe is. Also, is the QT_INIT_REASOURCE() necessary? I don't have it, which could explain why the image in the qrc file is not showing. However, the image did show up in the button in qt creator.

                It would be quite funny If I wasn't able to load my image because I was loading the image wrong both from file and qrc

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  A shadow build is a build directory outside your sources (which is the default option when using QtCreator)

                  QT_INIT_RESOURCE is only needed if you have the resources in a static library.

                  That's why I generally use absolute file path if the files are not in resources (even if the file is in the application dir, the working directory might be changed so...)

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    joshua-anderson
                    wrote on last edited by
                    #9

                    Good News! I followed your advice and use a absolute path when loading the image directly from disk and it works.

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      joshua-anderson
                      wrote on last edited by
                      #10

                      Wired. I can set the image just fine from a file absolute path (like c:\test.png) in the ui file, but when I try to set the image from file absoulte path from the qpixmap code like above, I am getting a null pixmap. Any clue why a pah that works in the ui file would not load in a QPixmap?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Indeed it's strange.

                        Try to open the file with QFile (at the same place you create the pixmap), see if it fails and what error you get

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          joshua-anderson
                          wrote on last edited by
                          #12

                          If I use the code:
                          @QFile testing("C:\test.png");
                          qDebug() << testing.exists();@

                          Where my qpixmap code is (for reference I have tried it in my constructor and my resizeEvent function) the console prints false, indicating that something is really screwed up. I am 100% certain this file exists as this path will load a image from the ui file.

                          The following ui code:

                          @<iconset><normaloff>C:\test.png</normaloff>C:\test.png</iconset>@

                          displays the image correctly.

                          Any tips on things I could have screwed up in my code that could cause such a weird thing?

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            joshua-anderson
                            wrote on last edited by
                            #13

                            Hello! If anyone is interested or willing to take a look at my code (it's not much) https://dl.dropboxusercontent.com/u/57778776/Working-Copy-Trackbox.zip
                            I would really appreciate it. The code that is giving my problems is in trackboxmainwindow.cpp and is proceded by a large comment blocks. Thanks. I am so confused in the ways a exact path of a existing file can't be found by qt.

                            1 Reply Last reply
                            0
                            • musimbateM Offline
                              musimbateM Offline
                              musimbate
                              wrote on last edited by
                              #14

                              Hi ,try this:
                              1.Clean the whole project
                              2.Run qmake
                              3.Rebuild the whole project

                              your problem sounds like "this":http://qt-project.org/forums/viewthread/28815/

                              Hope this helps.

                              Why join the navy if you can be a pirate?-Steve Jobs

                              1 Reply Last reply
                              0
                              • J Offline
                                J Offline
                                joshua-anderson
                                wrote on last edited by
                                #15

                                I will try the clean advice(never a bad idea). This might be part of the problem with my issues in my reasources file, and this will hopefully fix it. Right now I am trying top figure out why qpixmapand qfile thinks a file with a direct path to disk does not exist when the same path works in qtcreator.

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  It's normal it's printing false. You're not escaping the \ in your code.

                                  Anyway since you're using Qt, use the forward slash (yes, like unix) for your paths everywhere and you wont be hit again by this.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • J Offline
                                    J Offline
                                    joshua-anderson
                                    wrote on last edited by
                                    #17

                                    Duh! I feel like a idiot for not noticing that. That should solve the loading image from disk problem. Next, to see if I can load from reasources.

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      If you have your images in a folder, don't forget to add it in your path

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • J Offline
                                        J Offline
                                        joshua-anderson
                                        wrote on last edited by
                                        #19

                                        musimbate, Thank you so much for your advice. It solved my resource loading problem. Why must you be this way windows?!?

                                        1 Reply Last reply
                                        0
                                        • SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          That's a question for Microsoft :D

                                          Anyway, if you have everything working now, don't forget to update the thread title prepending [solved] so other forum users may know a solution has been found :)

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved