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. How to convert QPixmap to base64 QString in Qt?

How to convert QPixmap to base64 QString in Qt?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.5k Views 2 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.
  • S Offline
    S Offline
    sigmind
    wrote on last edited by
    #1

    I am using QT 5.7 for a program where I have to convert a QPixmap to base64 QString format. I have tried to first convert the QPixmap to cv::Mat and then added my existing conversion flow.

    cv::Mat pixData(pix.rows(),pix.cols(),CV_8UC3,pix.scanline());
                std::vector<uchar> IMbuffer;
              //  std::vector<uint8_t> IMbuffer;
                cv::imencode(".png", pixData, IMbuffer);
                QByteArray byteArray = QByteArray::fromRawData((const char*)IMbuffer.data(), IMbuffer.size());
                QString base64Image(byteArray.toBase64());
    

    But it returns error:

    error: 'class QPixmap' has no member named 'rows'
        cv::Mat pixData(pix.rows(),pix.cols(),CV_8UC3,pix.scanline());
                            ^
    

    So it is clear that such conversion from QPixmap to cv::Mat is incompatible. So is there any easy way to convert QPixmap to base64 QString?

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

      Hi,

      QPixmap::size is likely what you are looking for.

      But there's no need to have OpenCV involved. Take a look at QPixmap::save

      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
      3
      • S Offline
        S Offline
        sigmind
        wrote on last edited by sigmind
        #3

        @SGaist
        I have tried from here

        QPixmap pix;
        QImage image = pix.toImage();
         QByteArray byteArray;
        
         QBuffer buffer(&byteArray);
        
            image.save(&buffer, "JPEG"); // writes the image in JPEG format inside the buffer
        
            QString iconBase64 = QString::fromLatin1(byteArray.toBase64().data());
        

        But it returns:

         error: 'QBuffer' was not declared in this scope
                    QBuffer buff(&byteArray);
                    ^
        
        JKSHJ 1 Reply Last reply
        0
        • S sigmind

          @SGaist
          I have tried from here

          QPixmap pix;
          QImage image = pix.toImage();
           QByteArray byteArray;
          
           QBuffer buffer(&byteArray);
          
              image.save(&buffer, "JPEG"); // writes the image in JPEG format inside the buffer
          
              QString iconBase64 = QString::fromLatin1(byteArray.toBase64().data());
          

          But it returns:

           error: 'QBuffer' was not declared in this scope
                      QBuffer buff(&byteArray);
                      ^
          
          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by JKSH
          #4

          @sigmind said in How to convert QPixmap to base64 QString in Qt?:

           error: 'QBuffer' was not declared in this scope
                      QBuffer buff(&byteArray);
                      ^
          

          Your compiler is telling you that you need to #include the QBuffer header.

          QPixmap pix;
          QImage image = pix.toImage();
          QByteArray byteArray;
          QBuffer buffer(&byteArray);
          

          Why not use @SGaist's suggestion? QPixmap::save()

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          2

          • Login

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