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 can I read QPixmap from QBuffer ?
QtWS25 Last Chance

How can I read QPixmap from QBuffer ?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 1.7k 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.
  • O Offline
    O Offline
    onurcevik
    wrote on 15 Dec 2018, 14:04 last edited by
    #1

    I am trying to read image from a buffer and display it on QGraphicsView. The reason I am doing this because I am trying to implement a UDP image streaming program.

    With the code below I am trying to capture image to the buffer and then read image from buffer to display it.

        QObject::connect(cap, &QCameraImageCapture::imageCaptured, [=] (int id, QImage img) {
            QByteArray buf;
            QBuffer buffer(&buf);
            buffer.open(QIODevice::WriteOnly);
            img.save(&buffer, "BMP");
    
            QPixmap *pixmap = new QPixmap();
            pixmap->loadFromData(QPixmap::loadFromData(buffer.buffer()));
            scene->addPixmap(*pixmap);
    
    
        });
    
    

    But it gives this error:

    error: cannot call member function 'bool QPixmap::loadFromData(const QByteArray&, const char*, Qt::ImageConversionFlags)' without object
           pixmap->loadFromData(QPixmap::loadFromData(buffer.buffer()));
                                                                     ^
    

    Can you help me with this ?

    1 Reply Last reply
    0
    • O Offline
      O Offline
      onurcevik
      wrote on 15 Dec 2018, 17:05 last edited by
      #7

      I realised I was nesting two objects with this code:

      pixmap->loadFromData(QPixmap::loadFromData(buffer.buffer()));
      

      I edited my code like this and solved my problem.

          QPixmap *pixmap = new QPixmap();
          pixmap->loadFromData(buffer.buffer());
          scene->addPixmap(*pixmap);
      
      1 Reply Last reply
      0
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 15 Dec 2018, 15:12 last edited by Christian Ehrlicher
        #2

        QPixmap::loadFromData is not a static function so you'll need an object. The whole stuff is also not needed - why do you save a QImage in a QBuffer just to load it again into a QPixmap?

        QObject::connect(cap, &QCameraImageCapture::imageCaptured, [this] (int id, const QImage &img) {
          scene->addPixmap(QPixmap::fromImage(img));
        }
        

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        O 1 Reply Last reply 15 Dec 2018, 15:53
        2
        • C Christian Ehrlicher
          15 Dec 2018, 15:12

          QPixmap::loadFromData is not a static function so you'll need an object. The whole stuff is also not needed - why do you save a QImage in a QBuffer just to load it again into a QPixmap?

          QObject::connect(cap, &QCameraImageCapture::imageCaptured, [this] (int id, const QImage &img) {
            scene->addPixmap(QPixmap::fromImage(img));
          }
          
          O Offline
          O Offline
          onurcevik
          wrote on 15 Dec 2018, 15:53 last edited by
          #3

          @Christian-Ehrlicher The reason I save QImage to Buffer and load it again QPixmap is I am trying to implement a UDP image streaming program like I stated in my question above. So I am testing if I can load/read images then I'll try to implement UDP socket part.

          So your code doesn't help me at all :) . You are loading QImage not reading it from buffer.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 15 Dec 2018, 16:11 last edited by
            #4

            @onurcevik said in How can I read QPixmap from QBuffer ?:

            So your code doesn't help me at all :)

            Maybe not my code but my comment:

            QPixmap::loadFromData is not a static function so you'll need an object.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            O 1 Reply Last reply 15 Dec 2018, 16:43
            0
            • C Christian Ehrlicher
              15 Dec 2018, 16:11

              @onurcevik said in How can I read QPixmap from QBuffer ?:

              So your code doesn't help me at all :)

              Maybe not my code but my comment:

              QPixmap::loadFromData is not a static function so you'll need an object.

              O Offline
              O Offline
              onurcevik
              wrote on 15 Dec 2018, 16:43 last edited by
              #5

              @Christian-Ehrlicher How can I use object in this code ? Should I create QPixmap Object to access it ? Can you show it with code ?

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 15 Dec 2018, 16:53 last edited by
                #6

                @onurcevik said in How can I read QPixmap from QBuffer ?:

                How can I use object in this code ?

                QPixmap pm;

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                2
                • O Offline
                  O Offline
                  onurcevik
                  wrote on 15 Dec 2018, 17:05 last edited by
                  #7

                  I realised I was nesting two objects with this code:

                  pixmap->loadFromData(QPixmap::loadFromData(buffer.buffer()));
                  

                  I edited my code like this and solved my problem.

                      QPixmap *pixmap = new QPixmap();
                      pixmap->loadFromData(buffer.buffer());
                      scene->addPixmap(*pixmap);
                  
                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 15 Dec 2018, 18:55 last edited by
                    #8

                    @onurcevik said in How can I read QPixmap from QBuffer ?:

                    QPixmap *pixmap = new QPixmap();

                    And where do you delete this? Now you've a classic memleak. It's not needed to create it on the heap at all...

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    2

                    3/8

                    15 Dec 2018, 15:53

                    topic:navigator.unread, 5
                    • Login

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