How can I read QPixmap from QBuffer ?
-
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 ?
-
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);
-
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)); }
-
@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.
-
@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.
-
@Christian-Ehrlicher How can I use object in this code ? Should I create QPixmap Object to access it ? Can you show it with code ?
-
@onurcevik said in How can I read QPixmap from QBuffer ?:
How can I use object in this code ?
QPixmap pm;
-
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);
-
@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...