PDA

View Full Version : How to save widgets in layout to single file



davethomaspilot
24th September 2014, 12:35
I have a window that contains a horizontal layout that consists of 1-6 widgets. Each widget is a simple label with a pixmap.

Basically, the window consists of from 1-6 images that scale so they fit horizontally in a fixed window size.

I'd like to save a "composite image" to a file that's what's seen inside the window's horizontal layout.

I've seen examples on how to do this in Qt 5:


myWidget->grab().save("image.png");

but I need to do it in Qt4.8.

Is there a similar idiom to do it in Qt4?

Thanks,

Dave Thomas

Added after 17 minutes:

Actually, the layout always contains six widgets. But, only 1-6 are set to be visible.

That's how the scaling is done so they fit the window size.

Rajesh.Rathod
24th September 2014, 12:55
read documentation about QPixmap::grabWidget(...) function, it will return QPixmap and QPixmap has function to save it.

davethomaspilot
24th September 2014, 14:22
There are N pixmaps, one associated with each widget in the layout.

I'm looking for a combined pixmap of all the widgets contained in the layout.

Of course I can write code to use QPainter to build it from each pixmap, eg (untested):


QImage *compositeImage = new QImage(subImageWidth*subImages.count(),subImages[0].height(),QImage::Format_RGB888);

QString fname = fg->getSaveFileName();
QPainter painter(compositeImage);

for (int i=0;i<subImages.count();i++)
painter.drawImage(i*subImageWidth,0,subImages[i]);

compositeImage->save(fname);
delete compositeImage;


I'm asking if there's an easier/better way to do it in Qt4 like is done in Qt5.

Added after 50 minutes:

Rajesh, I dismissed your good suggestion too quickly. I'm sorry!

I read the documentation you pointed me too, and I think it is what I need.

I'll give it a try!

Thanks,

Dave Thomas

d_stranz
25th September 2014, 02:24
Just be aware that QPixmap::grabWidget() is deprecated in Qt 5. Also, there is no need to allocate memory for the QImage (ie. using "new" as you do in line 1 above). You can simply declare a QImage on the stack and the memory will be freed up when it goes out of scope at the end of the method.

wysota
26th September 2014, 00:06
Of course I can write code to use QPainter to build it from each pixmap
And that is what you should do.