2 Attachment(s)
Re: Saving qwt plot as image
Ok, I forgot to give the size of the image.
Code:
...
print(pixmap,filter);
if( pixmap.save(fileName, "png" ))
qDebug()<<"OK";
else
qDebug()<<"Uhm...";
...
But I get an image without the scales!!
Re: Saving qwt plot as image
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
1 Attachment(s)
Re: Saving qwt plot as image
Nothing.
With Qt::red I get the following image.
Re: Saving qwt plot as image
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
Re: Saving qwt plot as image
Hi there,
I use the following code without any problems, hope this helps:
Code:
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 jpegExt
=".jpeg", pngExt
=".png", tifExt
=".tif", bmpExt
=".bmp", tif2Ext
="tiff";
// Suffix for the files QString suggestedName
=restorePath
().
replace("flxhst",
"jpeg");
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" );
}
}
Re: Saving qwt plot as image
Quote:
Originally Posted by
quashie
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
Re: Saving qwt plot as image-QwtPlot::print() bug
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.
Code:
void MyPlot::exportSVG()
{
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));
filter.setOptions(options);
painter.drawPixmap(0,0,-1,-1,pixmap);
// print(&painter,QRect(0,0,800,600),filter);:confused:
}
}
void MyPlot::exportPNG()
fileName
= QFileDialog::getSaveFileName(this, tr
("Nome file da esportare"),
QString(),
"Graphic files (*.png )");
if ( !fileName.isEmpty() )
{
// int options = QwtPlotPrintFilter::PrintAll;
// int options = ~QwtPlotPrintFilter::PrintBackground;
filter.setOptions(options);
//print(pixmap,filter );:confused:
if ( pixmap.save(fileName, "png" ))
qDebug()<<"ok";
else
qDebug()<<"Uhmm";
}
}
Re: Saving qwt plot as image-QwtPlot::print() bug
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:
Code:
void MainWin::print()
{
pixmap.fill(Qt::white); // Qt::transparent ?
filter.setOptions(options);
d_plot->print(pixmap, filter);
label->setPixmap(pixmap);
label->show();
}
Here everything ( including the scales) is on a white background, like expected.
Uwe
Re: Saving qwt plot as image
Quote:
Originally Posted by
Uwe
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
Re: Saving qwt plot as image-QwtPlot::print() bug
Quote:
Originally Posted by
Uwe
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....
Re: Saving qwt plot as image-QwtPlot::print() bug
Try Qwt from SVN ( 5.1 branch ).
Uwe
Re: Saving qwt plot as image
Great! Now all works!
Thank you.
Re: Saving qwt plot as image-QwtPlot::print() bug
Quote:
Originally Posted by
giusepped
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.
Code:
void MyPlot::exportSVG()
{
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));
filter.setOptions(options);
painter.drawPixmap(0,0,-1,-1,pixmap);
// print(&painter,QRect(0,0,800,600),filter);:confused:
}
}
void MyPlot::exportPNG()
fileName
= QFileDialog::getSaveFileName(this, tr
("Nome file da esportare"),
QString(),
"Graphic files (*.png )");
if ( !fileName.isEmpty() )
{
// int options = QwtPlotPrintFilter::PrintAll;
// int options = ~QwtPlotPrintFilter::PrintBackground;
filter.setOptions(options);
//print(pixmap,filter );:confused:
if ( pixmap.save(fileName, "png" ))
qDebug()<<"ok";
else
qDebug()<<"Uhmm";
}
}
thanks for this post