PDA

View Full Version : Saving qwt plot as image



giusepped
17th December 2008, 01:09
I don't know if somebodyelse did the same question, but I'am unable to save the plot in other formats.
In my function I have


fileName = QFileDialog::getSaveFileName(this, tr("File name"), QString(), "Graphic files (*.svg,*png)");
QImage pixmap;
print(pixmap);

if( pixmap.save(fileName, "PNG" ))
qDebug()<<"Saved";
else
qDebug()<<"Not saved";

}


I always get "Not saved". Why?
G

giusepped
17th December 2008, 01:40
Ok, I forgot to give the size of the image.


...
int options = QwtPlotPrintFilter::PrintAll;
options &= ~QwtPlotPrintFilter::PrintBackground;
options |= QwtPlotPrintFilter::PrintFrameWithScales;

QPainter painter(800,600,QImage::Format_RGB32);

print(pixmap,filter);

if( pixmap.save(fileName, "png" ))
qDebug()<<"OK";
else
qDebug()<<"Uhm...";
...

But I get an image without the scales!!

Uwe
17th December 2008, 06:16
Guess the scales are there, but you can't see them because they are painted in black on a black background. Initialize your image with a color (image.fill(Qt::red) ) before you print on it.

Uwe

giusepped
17th December 2008, 07:14
Nothing.
With Qt::red I get the following image.

quashie
19th December 2008, 23:52
I have the exact same problem when exporting plots to jpg/png/tiff via QImage or QPixmap. The Plot is there, but no axis.

Exporting to pdf/ps via QPrinter or to svg via QSvgGenerator works all fine.

I also tried the hint to fill the QImage red or white before writing the plot to it, but for me that didn't have any effect.


Michael

cnbp173
20th December 2008, 00:35
Hi there,

I use the following code without any problems, hope this helps:



qPix = QPixmap::grabWidget(plotWidget);
if(qPix.isNull()){
qDebug("Failed to capture the plot for saving");
return;
}
QString types( "JPEG file (*.jpeg);;" // Set up the possible graphics formats
"Portable Network Graphics file (*.png);;"
"Bitmap file (*.bmp)");
QString filter; // Type of filter
QString jpegExt=".jpeg", pngExt=".png", tifExt=".tif", bmpExt=".bmp", tif2Ext="tiff"; // Suffix for the files
QString suggestedName=restorePath().replace("flxhst","jpeg");
QString fn = QFileDialog::getSaveFileName(viewParent(),tr("Save Image"),
suggestedName,types,&filter);

if ( !fn.isEmpty() ) { // If filename is not a null
if (fn.contains(jpegExt)) { // Remove file extension is already there
fn.remove(jpegExt);
}
else if (fn.contains(pngExt)) {
fn.remove(pngExt);
}
else if (fn.contains(bmpExt)) {
fn.remove(bmpExt);
}
if (filter.contains(jpegExt)) { // OR, Test to see if jpeg and save
fn+=jpegExt;
qPix.save( fn, "JPEG" );
}
else if (filter.contains(pngExt)) { // OR, Test to see if png and save
fn+=pngExt;
qPix.save( fn, "PNG" );
}
else if (filter.contains(bmpExt)) { // OR, Test to see if bmp and save
fn+=bmpExt;
qPix.save( fn, "BMP" );
}
}

Uwe
20th December 2008, 09:37
I also tried the hint to fill the QImage red or white before writing the plot to it, but for me that didn't have any effect
Save your initialized image without printing to see if it is red then.

Uwe

giusepped
20th December 2008, 15:10
I think both print functions of QwtPlot have bugs.
I tried saving the images without printing as suggested by cnbp173, and all works.
Same for the svg. Not using the printing function but using the save function of the QPaintDevice seems to work. The only bad thing is that we get also the background color of the widget.

I attach the code I checked.



void MyPlot::exportSVG()
{
QString fileName;


fileName = QFileDialog::getSaveFileName(this, tr("Nome file da esportare"), QString(),"Graphic files ( *svg)");

if ( !fileName.isEmpty() )
{
QSvgGenerator generator;
generator.setFileName(fileName);
generator.setSize(QSize(1024, 800));
QwtPlotPrintFilter filter;
int options = QwtPlotPrintFilter::PrintAll;
options |= QwtPlotPrintFilter::PrintBackground;
options |= QwtPlotPrintFilter::PrintFrameWithScales| QwtPlotPrintFilter::PrintMargin;
filter.setOptions(options);
QPixmap pixmap = QPixmap::grabWidget(this);
QPainter painter(&generator);
painter.drawPixmap(0,0,-1,-1,pixmap);



// print(&painter,QRect(0,0,800,600),filter);:confused:


}


}
void MyPlot::exportPNG()
{ QString fileName;

fileName = QFileDialog::getSaveFileName(this, tr("Nome file da esportare"), QString(),"Graphic files (*.png )");

if ( !fileName.isEmpty() )
{
QwtPlotPrintFilter filter;
// int options = QwtPlotPrintFilter::PrintAll;
// int options = ~QwtPlotPrintFilter::PrintBackground;
int options = QwtPlotPrintFilter::PrintFrameWithScales;
options |= QwtPlotPrintFilter::PrintBackground;
filter.setOptions(options);

QPixmap pixmap= QPixmap::grabWidget(this);


//print(pixmap,filter );:confused:

if ( pixmap.save(fileName, "png" ))
qDebug()<<"ok";
else
qDebug()<<"Uhmm";
}
}

Uwe
20th December 2008, 18:13
Grabbing a widget is like a screenshot - something completely different than rendering the plot to a paint device. By grabbing you can't something else than the current geometry of the plot widget and you never get a scalable vector format like PDF or SVG.

You didn't write for which purpose you need to export the plot. If your intention is a screenshot grabbing is o.k. If you want to export it to use it in other documents a scalable vector format is by far the better option.

For SVG the problem is the missing clipping in Qt, I already wrote how to work around this problem.

To check QImage/QPixmap I replaced MainWin::print in the bode example with the following code:


void MainWin::print()
{
QPixmap pixmap(600, 600);
pixmap.fill(Qt::white); // Qt::transparent ?

QwtPlotPrintFilter filter;
int options = QwtPlotPrintFilter::PrintAll;
options &= ~QwtPlotPrintFilter::PrintBackground;
options |= QwtPlotPrintFilter::PrintFrameWithScales;
filter.setOptions(options);

d_plot->print(pixmap, filter);

QLabel *label = new QLabel(NULL);
label->setPixmap(pixmap);
label->show();
}

Here everything ( including the scales) is on a white background, like expected.

Uwe

quashie
21st December 2008, 22:50
Save your initialized image without printing to see if it is red then.

Uwe


Indeed there was some problem and the image didn't get filled with the color.

Now it works all fine! The images gets filled white and the scales are visible...

Thanks a lot!

Michael

giusepped
22nd December 2008, 07:44
For SVG the problem is the missing clipping in Qt, I already wrote how to work around this problem.


[/CODE]



Uwe

Thank you Uwe for your time.
It will be very helpful if you can help us to patch the svg export. Yes, indeed I need also svg support in order to use plots inside other documents. And fot that, I still cannot understand why the clipping is not correct.
I tried to look inside the code, but I cannot understand where to patch.
Sorry for my low knoledge of software debugging....

Uwe
22nd December 2008, 08:22
Try Qwt from SVN ( 5.1 branch ).

Uwe

giusepped
22nd December 2008, 13:43
Great! Now all works!
Thank you.

umulingu
14th July 2009, 07:39
I think both print functions of QwtPlot have bugs.
I tried saving the images without printing as suggested by cnbp173, and all works.
Same for the svg. Not using the printing function but using the save function of the QPaintDevice seems to work. The only bad thing is that we get also the background color of the widget.

I attach the code I checked.



void MyPlot::exportSVG()
{
QString fileName;


fileName = QFileDialog::getSaveFileName(this, tr("Nome file da esportare"), QString(),"Graphic files ( *svg)");

if ( !fileName.isEmpty() )
{
QSvgGenerator generator;
generator.setFileName(fileName);
generator.setSize(QSize(1024, 800));
QwtPlotPrintFilter filter;
int options = QwtPlotPrintFilter::PrintAll;
options |= QwtPlotPrintFilter::PrintBackground;
options |= QwtPlotPrintFilter::PrintFrameWithScales| QwtPlotPrintFilter::PrintMargin;
filter.setOptions(options);
QPixmap pixmap = QPixmap::grabWidget(this);
QPainter painter(&generator);
painter.drawPixmap(0,0,-1,-1,pixmap);



// print(&painter,QRect(0,0,800,600),filter);:confused:


}


}
void MyPlot::exportPNG()
{ QString fileName;

fileName = QFileDialog::getSaveFileName(this, tr("Nome file da esportare"), QString(),"Graphic files (*.png )");

if ( !fileName.isEmpty() )
{
QwtPlotPrintFilter filter;
// int options = QwtPlotPrintFilter::PrintAll;
// int options = ~QwtPlotPrintFilter::PrintBackground;
int options = QwtPlotPrintFilter::PrintFrameWithScales;
options |= QwtPlotPrintFilter::PrintBackground;
filter.setOptions(options);

QPixmap pixmap= QPixmap::grabWidget(this);


//print(pixmap,filter );:confused:

if ( pixmap.save(fileName, "png" ))
qDebug()<<"ok";
else
qDebug()<<"Uhmm";
}
}





thanks for this post