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. Show picture using QLabel and Pixmap

Show picture using QLabel and Pixmap

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 8 Posters 47.0k Views 1 Watching
  • 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.
  • F Offline
    F Offline
    Fedi
    wrote on last edited by Fedi
    #1

    I'm beginner in Qt and I couldn't display a picture using QPixmap and QLabel.
    The code I wrote is :
    QLabel *img = new QLabel(tab);
    img->setPixmap(QPixmap(":/Users/Learner/Desktop/photo.png"));
    tab is a pointer to the parent widget.
    I don't know what's wrong with this code , there is no compilation error but , the picture is not displayed . Is there anyone who can help me ??

    jsulmJ joeQJ YashpalY 3 Replies Last reply
    0
    • F Fedi

      I'm beginner in Qt and I couldn't display a picture using QPixmap and QLabel.
      The code I wrote is :
      QLabel *img = new QLabel(tab);
      img->setPixmap(QPixmap(":/Users/Learner/Desktop/photo.png"));
      tab is a pointer to the parent widget.
      I don't know what's wrong with this code , there is no compilation error but , the picture is not displayed . Is there anyone who can help me ??

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Fedi said in Show picture using QLabel and Pixmap:

      :/Users/Learner/Desktop/photo.png

      Is your image in a resource file or are you trying to load a file from the file system? If file system then you're missing the drive letter.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply
      1
      • F Fedi

        I'm beginner in Qt and I couldn't display a picture using QPixmap and QLabel.
        The code I wrote is :
        QLabel *img = new QLabel(tab);
        img->setPixmap(QPixmap(":/Users/Learner/Desktop/photo.png"));
        tab is a pointer to the parent widget.
        I don't know what's wrong with this code , there is no compilation error but , the picture is not displayed . Is there anyone who can help me ??

        joeQJ Offline
        joeQJ Offline
        joeQ
        wrote on last edited by
        #3

        @Fedi Hi, friend, welcome.

        You can reference code snippet below.

            QString filename = "xxxxx";
            QLabel* lbl = new QLabel(this);
            /** set content to show center in label */
            lbl->setAlignment(Qt::AlignCenter);
            QPixmap pix;
        
            /** to check wether load ok */
            if(pix.load(filename)){
                /** scale pixmap to fit in label'size and keep ratio of pixmap */
                pix = pix.scaled(lbl->size(),Qt::KeepAspectRatio);
                lbl->setPixmap(pix);
            }
        

        Just do it!

        F 1 Reply Last reply
        3
        • Vinod KuntojiV Offline
          Vinod KuntojiV Offline
          Vinod Kuntoji
          wrote on last edited by
          #4

          @Fedi ,

          Your pixmap is not scaled.

          QPixmap pix(":/Users/Learner/Desktop/photo.png");
          int w = img->width ();
          int h = img->height ();
          img->setPixmap (pix.scaled (w,h,Qt::KeepAspectRatio));

          C++, Qt, Qt Quick Developer,
          PthinkS, Bangalore

          1 Reply Last reply
          1
          • F Fedi

            I'm beginner in Qt and I couldn't display a picture using QPixmap and QLabel.
            The code I wrote is :
            QLabel *img = new QLabel(tab);
            img->setPixmap(QPixmap(":/Users/Learner/Desktop/photo.png"));
            tab is a pointer to the parent widget.
            I don't know what's wrong with this code , there is no compilation error but , the picture is not displayed . Is there anyone who can help me ??

            YashpalY Offline
            YashpalY Offline
            Yashpal
            wrote on last edited by
            #5

            @Fedi

            • Make sure your label or it's parent widget is visible.

            • Make sure QPixmap object is not NULL.

            • Try whether plain text appears on QLabel.

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

              Hi and welcome to devnet,

              As @jsulm asked, are you using an image using Qt's resource system ?

              If not then:

              • If you are running Windows, then @jsulm is right regarding the missing letter
              • If you are running macOS, then drop the :.

              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
              5
              • AmoghA Offline
                AmoghA Offline
                Amogh
                wrote on last edited by
                #7
                QPixmap m_logo_pic;
                QLabel *m_pic_label;
                m_pic_label=new QLabel();
                
                    m_logo_pic.load(":/image/bank.png");
                    m_pic_label->setPixmap(m_logo_pic);
                ////Place the labe in the layout
                
                
                1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Fedi said in Show picture using QLabel and Pixmap:

                  :/Users/Learner/Desktop/photo.png

                  Is your image in a resource file or are you trying to load a file from the file system? If file system then you're missing the drive letter.

                  F Offline
                  F Offline
                  Fedi
                  wrote on last edited by
                  #8

                  @jsulm you are right bro , now it works ,thx :))

                  1 Reply Last reply
                  1
                  • Pradeep KumarP Offline
                    Pradeep KumarP Offline
                    Pradeep Kumar
                    wrote on last edited by
                    #9

                    Hi,

                    @Fedi , glad u got solution , can u mark as solved.

                    Thanks,

                    Pradeep Kumar
                    Qt,QML Developer

                    1 Reply Last reply
                    0
                    • joeQJ joeQ

                      @Fedi Hi, friend, welcome.

                      You can reference code snippet below.

                          QString filename = "xxxxx";
                          QLabel* lbl = new QLabel(this);
                          /** set content to show center in label */
                          lbl->setAlignment(Qt::AlignCenter);
                          QPixmap pix;
                      
                          /** to check wether load ok */
                          if(pix.load(filename)){
                              /** scale pixmap to fit in label'size and keep ratio of pixmap */
                              pix = pix.scaled(lbl->size(),Qt::KeepAspectRatio);
                              lbl->setPixmap(pix);
                          }
                      
                      F Offline
                      F Offline
                      Fedi
                      wrote on last edited by
                      #10

                      @joeQ I've just added the drive letter to my code and it works ! the solution you gave me works as well, thx a lot :D

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

                        Don't forget that using such a hardcoded value won't make your application portable.

                        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
                        1

                        • Login

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