Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel::setPixmap() doesn't show image!
Qt 6.12 LTS is out! See what's new in the release blog

QLabel::setPixmap() doesn't show image!

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 15.2k 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.
  • D Offline
    D Offline
    David406
    wrote on last edited by David406
    #1

    Hi, I'm using setPixmap() method to draw images on a label, but met a problem. Originally, the label has its background set with a pixmap, let call it pixmap1, with setPixmap(pixmap1). Then I copy this pixmap and use it as the paint device to draw images on it. After painting is done, I then reset this label's pixmap with this modified copy, let's call it pixmap2, using its set method setPixmap(pixmap2). However, the pixmap on the label wouldn't change in any way. What's strange is that when I save this label's modified pixmap into a png file, everything is all right, and the saved pixmap is correctly modified as I paint images on it.

    I really have no idea what's going wrong here. Could anyone give some suggestions?

    In case you need it, here's the code snippet:

        else if (status == ScreenShot::IsToDrawLine && me->buttons() == Qt::LeftButton)
        {
            drawLine(pixmapForDrawing, me->pos());
            selectedImageLabel->setPixmap(pixmapForDrawing);
            QString filename = "D:\\test.png";
            (selectedImageLabel->pixmap())->save(filename);
        }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      continuation from https://forum.qt.io/topic/77745/setpixmap-doesn-t-show-image

      This is not the final solution, by any means but could you add QApplication::ProcessEvents(QEventLoop::ExcludeUserInputEvents); after selectedImageLabel->setPixmap(pixmapForDrawing);? and see if the image changes?

      Where are you calling this snippet from?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      D 2 Replies Last reply
      1
      • VRoninV VRonin

        continuation from https://forum.qt.io/topic/77745/setpixmap-doesn-t-show-image

        This is not the final solution, by any means but could you add QApplication::ProcessEvents(QEventLoop::ExcludeUserInputEvents); after selectedImageLabel->setPixmap(pixmapForDrawing);? and see if the image changes?

        Where are you calling this snippet from?

        D Offline
        D Offline
        David406
        wrote on last edited by David406
        #3

        @VRonin
        Thank you!

        This snippet is in the overloaded mouseMoveEvent(QMouseEvent *me) function.

        In my last post several days ago, according to your suggestion, I correctly set the pixmap of a label by avoiding subclassing QLabel. But here, I met similar problem again! I couldn't reset QLabel's pixmap. Anyways, I will try your suggestion and let's see the result!

        1 Reply Last reply
        0
        • VRoninV VRonin

          continuation from https://forum.qt.io/topic/77745/setpixmap-doesn-t-show-image

          This is not the final solution, by any means but could you add QApplication::ProcessEvents(QEventLoop::ExcludeUserInputEvents); after selectedImageLabel->setPixmap(pixmapForDrawing);? and see if the image changes?

          Where are you calling this snippet from?

          D Offline
          D Offline
          David406
          wrote on last edited by David406
          #4

          @VRonin said in QLabel::setPixmap() doesn't show image!:

          QApplication::ProcessEvents(QEventLoop::ExcludeUserInputEvents);

          Hi, I added this to the code, but still didn't work.

          J.HilkJ 1 Reply Last reply
          0
          • D David406

            @VRonin said in QLabel::setPixmap() doesn't show image!:

            QApplication::ProcessEvents(QEventLoop::ExcludeUserInputEvents);

            Hi, I added this to the code, but still didn't work.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @David406
            hi,
            maybe you haven't released your pixmap correctly when you're finished with the drawing?

            You could also try to convert you QPixmap to a QImage, as a step in between

            QImage = pixmapForDrawing.toImage();
            selectedImageLabel->setPixmap( QPixmap::fromImage(pixmapForDrawing));
            

            to see, if that changes anything.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            D 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @David406
              hi,
              maybe you haven't released your pixmap correctly when you're finished with the drawing?

              You could also try to convert you QPixmap to a QImage, as a step in between

              QImage = pixmapForDrawing.toImage();
              selectedImageLabel->setPixmap( QPixmap::fromImage(pixmapForDrawing));
              

              to see, if that changes anything.

              D Offline
              D Offline
              David406
              wrote on last edited by David406
              #6

              @J.Hilk
              Thanks.
              Did you mean:

                      drawLine(pixmapForDrawing, me->pos());
                      QImage imageForDrawing = pixmapForDrawing.toImage();
                      selectedImageLabel->setPixmap(QPixmap::fromImage(imageForDrawing));
                      QString filename = "D:\\test.png";
                      (selectedImageLabel->pixmap())->save(filename);
              

              This didn't work. The saved png file is right, but not showing on the label.

              Oh, this is my drawLine method, although I don't think it's relevent:

              void ScreenShot::drawLine(QPixmap &pixmap, const QPoint &endPoint)
              {
                  QPainter painter(&pixmap);
                  painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
                                      Qt::RoundJoin));
                  painter.drawLine(selectedImageLabel->mapFromGlobal(lastPenPoint),
                                   selectedImageLabel->mapFromGlobal(endPoint));
                  //painter.drawLine(QPoint(0, 0), QPoint(1000, 1000));
                  lastPenPoint = endPoint;
              }
              
              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                @David406 said in QLabel::setPixmap() doesn't show image!:

                avoiding subclassing QLabel. But here, I met similar problem again!

                Are you subclassing QLabel? could you show us the code of that subclass?

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                D 1 Reply Last reply
                0
                • VRoninV VRonin

                  @David406 said in QLabel::setPixmap() doesn't show image!:

                  avoiding subclassing QLabel. But here, I met similar problem again!

                  Are you subclassing QLabel? could you show us the code of that subclass?

                  D Offline
                  D Offline
                  David406
                  wrote on last edited by David406
                  #8

                  @VRonin
                  No, the selectedImageLabel is just an instance of QLabel, not a subclass instance. It's created as a member in the ScreenShot class:

                  QLabel *selectedImageLabel;
                  
                  J.HilkJ 1 Reply Last reply
                  0
                  • D David406

                    @VRonin
                    No, the selectedImageLabel is just an instance of QLabel, not a subclass instance. It's created as a member in the ScreenShot class:

                    QLabel *selectedImageLabel;
                    
                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @David406
                    ok to make sure, it works at all. Would this show the correct Image?

                    drawLine(pixmapForDrawing, me->pos());
                    QLabel *lbl = new QLabel();
                    lbl->setPixmap(pixmapForDrawing);
                    lbl->show();
                    

                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    D 1 Reply Last reply
                    1
                    • J.HilkJ J.Hilk

                      @David406
                      ok to make sure, it works at all. Would this show the correct Image?

                      drawLine(pixmapForDrawing, me->pos());
                      QLabel *lbl = new QLabel();
                      lbl->setPixmap(pixmapForDrawing);
                      lbl->show();
                      
                      D Offline
                      D Offline
                      David406
                      wrote on last edited by
                      #10

                      @J.Hilk
                      This works correctly: A series of windows pops up with lines painted on the original pixmap.

                      J.HilkJ 1 Reply Last reply
                      0
                      • D David406

                        @J.Hilk
                        This works correctly: A series of windows pops up with lines painted on the original pixmap.

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #11

                        @David406
                        Alright, we narrowed the problem down. It's with selectedImageLabel. My guess, if its a standard QLabel, the pixmap is somewhere else overwriten.


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        D 1 Reply Last reply
                        0
                        • J.HilkJ J.Hilk

                          @David406
                          Alright, we narrowed the problem down. It's with selectedImageLabel. My guess, if its a standard QLabel, the pixmap is somewhere else overwriten.

                          D Offline
                          D Offline
                          David406
                          wrote on last edited by
                          #12

                          @J.Hilk
                          Thanks.
                          Yes, it's the problem somewhere with selectedImageLabel. But I don't quite understand what you mean by saying overwritten. The saved pixmap of selectedImageLabel is all right. It just doesn't update and show up on the label.

                          J.HilkJ 1 Reply Last reply
                          0
                          • D David406

                            @J.Hilk
                            Thanks.
                            Yes, it's the problem somewhere with selectedImageLabel. But I don't quite understand what you mean by saying overwritten. The saved pixmap of selectedImageLabel is all right. It just doesn't update and show up on the label.

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #13

                            @David406
                            Out of the top of my head, I can only think of 3 reasons, why a QLabel wouldn't show a pixmap.

                            • It gets a new Pixmap(overwritten) before the paintevent could draw the old one
                            • The Lable is actually not visible. Other widget on top or hidden
                            • A stylesheet forces a different image to be drawn.

                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            D 1 Reply Last reply
                            1
                            • J.HilkJ J.Hilk

                              @David406
                              Out of the top of my head, I can only think of 3 reasons, why a QLabel wouldn't show a pixmap.

                              • It gets a new Pixmap(overwritten) before the paintevent could draw the old one
                              • The Lable is actually not visible. Other widget on top or hidden
                              • A stylesheet forces a different image to be drawn.
                              D Offline
                              D Offline
                              David406
                              wrote on last edited by David406
                              #14

                              @J.Hilk
                              Thanks.
                              The functionality of my code is:

                              • Select an area of the screen with mouse and set the selected image to label, move and show the label on the selected area dynamicly.
                              • After selection is done, images can be drawn on the label.

                              And now, the first part of my code works correctly, here is how it actually works:

                              alt text

                              In the above picture, the highlighted area is actually a label with selected image as its pixmap. So you can see the label is shown correctly.

                              However, the second part of my code doesn't work. Namely, pixmap on the label can't be updated (or reset) with its original pixmap modified.

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                David406
                                wrote on last edited by
                                #15

                                Thank you all, finally I found that the problem is in the implementation of paintEvent().

                                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
                                • Unsolved