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] PNG Pixmap in QLabel Won't Show
QtWS25 Last Chance

[Solved] PNG Pixmap in QLabel Won't Show

Scheduled Pinned Locked Moved General and Desktop
qwidgetqlabelimagepixmap
6 Posts 2 Posters 37.5k 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.
  • M Offline
    M Offline
    maximo
    wrote on 25 Aug 2015, 00:35 last edited by maximo
    #1

    I am a noob with Qt/C++. I am loading a transparent PNG in the Pixmap property of a QLabel widget. I can see it in QtCreator, but when I choose Build > Run, it does not display. The image is in the same directory as the project. When I was working on the Pixmap property, I clicked the popdown menu that let me select a file. I selected this local file and it showed up in the label -- but only in QtCreator, not when I run the application. Is this a known bug in Qt5.5 Open Source (Free) version or am I doing something wrong? BTW, I'm on a Mac Air.

    M 1 Reply Last reply 25 Aug 2015, 00:41
    0
    • M maximo
      25 Aug 2015, 00:35

      I am a noob with Qt/C++. I am loading a transparent PNG in the Pixmap property of a QLabel widget. I can see it in QtCreator, but when I choose Build > Run, it does not display. The image is in the same directory as the project. When I was working on the Pixmap property, I clicked the popdown menu that let me select a file. I selected this local file and it showed up in the label -- but only in QtCreator, not when I run the application. Is this a known bug in Qt5.5 Open Source (Free) version or am I doing something wrong? BTW, I'm on a Mac Air.

      M Offline
      M Offline
      maximus
      wrote on 25 Aug 2015, 00:41 last edited by
      #2

      @maximo

      Hi there,

      I use this code to set Pixmap on my QLabel. Seems to work for me!

      QPixmap pixmapTarget = QPixmap(":/image/icon/target");
      pixmapTarget = pixmapTarget.scaled(size-5, size-5, Qt::KeepAspectRatio, Qt::SmoothTransformation);
      ui->label_image_power->setPixmap(pixmapTarget );
      

      Have a great day,
      Max


      Free Indoor Cycling Software - https://maximumtrainer.com

      M 1 Reply Last reply 25 Aug 2015, 00:50
      0
      • M maximus
        25 Aug 2015, 00:41

        @maximo

        Hi there,

        I use this code to set Pixmap on my QLabel. Seems to work for me!

        QPixmap pixmapTarget = QPixmap(":/image/icon/target");
        pixmapTarget = pixmapTarget.scaled(size-5, size-5, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->label_image_power->setPixmap(pixmapTarget );
        

        Have a great day,
        Max

        M Offline
        M Offline
        maximo
        wrote on 25 Aug 2015, 00:50 last edited by maximo
        #3

        @maximus Thanks. It appears that this must be a bug in QtCreator. I was able to load the image properly by adding the following right after my ui->setupUi(this); line:

        ui->lblLogo->setPixmap(QPixmap("/Users/maximo/Projects/proj1/logo.png"));
        

        I had to use an absolute path. Is there any way that I can load this from a relative path from the application's own directory?

        M 1 Reply Last reply 25 Aug 2015, 01:04
        0
        • M maximo
          25 Aug 2015, 00:50

          @maximus Thanks. It appears that this must be a bug in QtCreator. I was able to load the image properly by adding the following right after my ui->setupUi(this); line:

          ui->lblLogo->setPixmap(QPixmap("/Users/maximo/Projects/proj1/logo.png"));
          

          I had to use an absolute path. Is there any way that I can load this from a relative path from the application's own directory?

          M Offline
          M Offline
          maximus
          wrote on 25 Aug 2015, 01:04 last edited by maximus
          #4

          @maximo

          Yes, check the Resource system, really useful, you can set your image in there and it will be bundled with your executable
          Good luck!


          Free Indoor Cycling Software - https://maximumtrainer.com

          M 1 Reply Last reply 25 Aug 2015, 01:21
          0
          • M maximus
            25 Aug 2015, 01:04

            @maximo

            Yes, check the Resource system, really useful, you can set your image in there and it will be bundled with your executable
            Good luck!

            M Offline
            M Offline
            maximo
            wrote on 25 Aug 2015, 01:21 last edited by
            #5

            @maximus Aha! Now I get it. I rightclicked the Projects > Sources in QtCreator, chose Add New... > Qt > Qt Resource File. I gave it a name of simply "res" (and it adds the extension .qrc on the end) and then saw a new Resources folder on the left underneath Forms in the QtCreator project editor. Then, in the resource editor, I clicked the Add popdown listbox and chose Files. I selected my file from the local application directory, although I guess I could have put it in an images folder in that application directory. Then, I went to my label control, cleared out the pixmap property, and chose the Add Resource option, and selected the resource (a little tricky, but I clicked around and found it). I then commented out my pixmap code that I had added in the main window because now I'm doing it the resource way. I then chose Build > Run and it worked! :)

            1 Reply Last reply
            1
            • M Offline
              M Offline
              maximus
              wrote on 25 Aug 2015, 01:39 last edited by
              #6

              Glad it worked! You can add [Solved] - in the title of this post to mark it has solved ;)


              Free Indoor Cycling Software - https://maximumtrainer.com

              1 Reply Last reply
              0

              1/6

              25 Aug 2015, 00:35

              • Login

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