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. Save a QPixmap in a label
Qt 6.11 is out! See what's new in the release blog

Save a QPixmap in a label

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 7.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.
  • M Offline
    M Offline
    MélissaQT
    wrote on last edited by
    #1

    Hello,

    I would like to save my picture (QPixmap) as a png in my label, when I finished to add lines and ellipses.
    Like describe in the commentaries:

    @
    //---------------------------------------------Class LabelImage
    class LabelImage: public QLabel
    {
    public:

    LabelImage()
    {
      this->setMouseTracking(true);
    };
    
    void paintEvent (QPaintEvent *event)
    {
         QLabel::paintEvent( event );
         QPainter painter(this);
             
              // LINES ANS ELLIPSES TREATMENT LOOPS
    
             // BACK UP HERE ! like:
             //monPixmap.save("/Users/.../Desktop/GUI Picture/Picture " + 
             //QString::number(NumberOfPicture) +".png","png",100);
                             
                    MesureFinished = true;
    }
    
    void mousePressEvent (QMouseEvent * event)
    {
        if (MesureFinished == false)
        {
            PixelColumn = event->x();
            PixelRow = event->y();
            this->update();
            QLabel::mousePressEvent( event );
        }
    };
    

    };
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MélissaQT
      wrote on last edited by
      #2

      and here the c++ code where I'm using the .h:
      @
      //---------------------------------------------PICTURE WINDOW
      PictureWindow::PictureWindow(QImage Image, int NumberOfPicture) : QWidget()
      {
      setFixedSize(840, 356);

      QPixmap monPixmap;
      
      LabelImage* LabelIMG = new LabelImage();
      
      LabelIMG-> setFixedSize(monPixmap.fromImage(Image).size());
      

      }
      @

      1 Reply Last reply
      0
      • F Offline
        F Offline
        flaviotordini
        wrote on last edited by
        #3

        No way to go. You have to draw in a QPixmap instead by doing something like:
        @QPainter p(pixmap);@
        Don't do it inside paintEvent.

        Then you can save the pixmap and display it in a QWidget inside paintEvent using painter.drawPixmap(). No need to subclass QLabel, just subclass QWidget.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
          wrote on last edited by
          #4

          I'm assuming that you have picture in png format. You need to load this in pixmap and set this to label. Is that correct ?

          1. Load the your png file to PixMap
          2. Set this PixMap to QLabel

          Subclassing QLabel and implementing paintEvent will re-draw you label.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          https://www.pthinks.com

          1 Reply Last reply
          0
          • IamSumitI Offline
            IamSumitI Offline
            IamSumit
            wrote on last edited by
            #5

            Try this..
            QString format = "png";

            QString fileName=CurrentDirPath+"/"+"image.png"; 
            
            QPixmap originalPixmap; 
            
            originalPixmap=QPixmap::grabWidget(label);
            
            
            if (!fileName.isEmpty())
            
                originalPixmap.save(fileName, format.toAscii());
            

            Be Cute

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MélissaQT
              wrote on last edited by
              #6

              Thanks all for those answers,

              [quote author="Dheerendra" date="1389809506"]I'm assuming that you have picture in png format. You need to load this in pixmap and set this to label. Is that correct ?

              1. Load the your png file to PixMap
              2. Set this PixMap to QLabel

              Subclassing QLabel and implementing paintEvent will re-draw you label.

              [/quote]

              I now back up the picture without figures (circles and ellipses) with this code:

              @

              //---------------------------------------------Class LabelImage
              class LabelImage: public QLabel
              {
              QPixmap mypix; //----------------------------------------------------NEW
              public:

              LabelImage()
              {
                mypix = ("..."); //---------------------NEW
                this->setPixmap(mypix);//---------------------NEW
              
                this->setMouseTracking(true);
              };
              
              void paintEvent (QPaintEvent *event)
              {
                   QLabel::paintEvent( event );
                   QPainter painter(this);
                       
                        // LINES ANS ELLIPSES TREATMENT LOOPS
                                       
                              MesureFinished = true;
                              mypix.save(".."); //--------------------------------------NEW
              }
              
              void mousePressEvent (QMouseEvent * event)
              {
                  if (MesureFinished == false)
                  {
                      PixelColumn = event->x();
                      PixelRow = event->y();
                      this->update();
                      QLabel::mousePressEvent( event );
                  }
              };
              

              };
              @

              How I can save picture + figures?

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

                Hi,

                Are you trying to create something like a Paint program ?

                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
                • M Offline
                  M Offline
                  MélissaQT
                  wrote on last edited by
                  #8

                  Hi SGaist,

                  Yes something like that

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

                    Then have a look at the Qt's Plug And Paint example, that will provide you a good ground to build on

                    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
                    • M Offline
                      M Offline
                      MélissaQT
                      wrote on last edited by
                      #10

                      Ok I will check. Thank you

                      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