PDA

View Full Version : saving QPixmap to QIODevice gives SEGV error



sattu
11th July 2017, 15:54
Hello all,

I am trying to capture a screenshot of my Application in a QPixmap and then save it to a QIODevice. But it's giving segmentation fault.
Not able to figure out why.

Here's the code-



QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId());

ui.myLabel->setPixmap(pix); //This is fine. Displays the Screenshot
ui.myLabel->setScaledContents(true);

QByteArray bArray;
QBuffer buffer(&bArray);
buffer.open(QIODevice::WriteOnly);

qDebug()<<"before";
pix.save(&buffer); //Gives SEGV Error
qDebug()<<"after";

buffer.close();




Not able to figure out what the problem is. :(

d_stranz
11th July 2017, 17:52
You haven't given a format in the save() call. Try using something like "PNG" as the second parameter.

sattu
12th July 2017, 09:39
Try using something like "PNG" as the second parameter.

Thanks a lot. That solved the issue but now I am facing another issue.
After saving the image file in PNG format, I am trying to read the same file in another application and again save it in another name. (Extremely sorry if it sounds strange but please bear with me :rolleyes: ) It's crashing.


The first app-


QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId());

QImage img = pix.toImage(); //Converting to QImage so that I can know which format it is in.

qDebug()<<"size: "<<img.size();
qDebug()<<"format: "<<img.format(); //returns QImage::Format_RGB16

img.save("/pefis/bin/test.png", "PNG"); //This works fine



In another app-


QImage img("/pefis/bin/test.png", "PNG");

qDebug()<<"size: "<<img.size();
qDebug()<<"format: "<<img.format(); //returns QImage::Format_RGB32

img.save("/pefis/bin/test1.png", "PNG"); //Crashes here. Gives SEGV Error



First the Image format is automatically changing and then it is crashing when I am trying to save it in another name.
Please point out what mistake I am committing.


-Thanking You,
sattu