Results 1 to 14 of 14

Thread: Saving qwt plot as image

  1. #1
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Saving qwt plot as image

    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
    Qt Code:
    1. fileName = QFileDialog::getSaveFileName(this, tr("File name"), QString(), "Graphic files (*.svg,*png)");
    2. QImage pixmap;
    3. print(pixmap);
    4.  
    5. if( pixmap.save(fileName, "PNG" ))
    6. qDebug()<<"Saved";
    7. else
    8. qDebug()<<"Not saved";
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 
    I always get "Not saved". Why?
    G

  2. #2
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Saving qwt plot as image

    Ok, I forgot to give the size of the image.
    Qt Code:
    1. ...
    2. int options = QwtPlotPrintFilter::PrintAll;
    3. options &= ~QwtPlotPrintFilter::PrintBackground;
    4. options |= QwtPlotPrintFilter::PrintFrameWithScales;
    5.  
    6. QPainter painter(800,600,QImage::Format_RGB32);
    7.  
    8. print(pixmap,filter);
    9.  
    10. if( pixmap.save(fileName, "png" ))
    11. qDebug()<<"OK";
    12. else
    13. qDebug()<<"Uhm...";
    14. ...
    To copy to clipboard, switch view to plain text mode 
    But I get an image without the scales!!
    Attached Images Attached Images

  3. #3
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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

  4. The following user says thank you to Uwe for this useful post:

    quashie (21st December 2008)

  5. #4
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Saving qwt plot as image

    Nothing.
    With Qt::red I get the following image.
    Attached Images Attached Images

  6. #5
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default 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

  7. #6
    Join Date
    May 2008
    Posts
    25
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Saving qwt plot as image

    Hi there,

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

    Qt Code:
    1. qPix = QPixmap::grabWidget(plotWidget);
    2. if(qPix.isNull()){
    3. qDebug("Failed to capture the plot for saving");
    4. return;
    5. }
    6. QString types( "JPEG file (*.jpeg);;" // Set up the possible graphics formats
    7. "Portable Network Graphics file (*.png);;"
    8. "Bitmap file (*.bmp)");
    9. QString filter; // Type of filter
    10. QString jpegExt=".jpeg", pngExt=".png", tifExt=".tif", bmpExt=".bmp", tif2Ext="tiff"; // Suffix for the files
    11. QString suggestedName=restorePath().replace("flxhst","jpeg");
    12. QString fn = QFileDialog::getSaveFileName(viewParent(),tr("Save Image"),
    13. suggestedName,types,&filter);
    14.  
    15. if ( !fn.isEmpty() ) { // If filename is not a null
    16. if (fn.contains(jpegExt)) { // Remove file extension is already there
    17. fn.remove(jpegExt);
    18. }
    19. else if (fn.contains(pngExt)) {
    20. fn.remove(pngExt);
    21. }
    22. else if (fn.contains(bmpExt)) {
    23. fn.remove(bmpExt);
    24. }
    25. if (filter.contains(jpegExt)) { // OR, Test to see if jpeg and save
    26. fn+=jpegExt;
    27. qPix.save( fn, "JPEG" );
    28. }
    29. else if (filter.contains(pngExt)) { // OR, Test to see if png and save
    30. fn+=pngExt;
    31. qPix.save( fn, "PNG" );
    32. }
    33. else if (filter.contains(bmpExt)) { // OR, Test to see if bmp and save
    34. fn+=bmpExt;
    35. qPix.save( fn, "BMP" );
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to cnbp173 for this useful post:

    giusepped (20th December 2008)

  9. #7
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Saving qwt plot as image

    Quote Originally Posted by quashie View Post
    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

  10. #8
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

    Qt Code:
    1. void MyPlot::exportSVG()
    2. {
    3. QString fileName;
    4.  
    5.  
    6. fileName = QFileDialog::getSaveFileName(this, tr("Nome file da esportare"), QString(),"Graphic files ( *svg)");
    7.  
    8. if ( !fileName.isEmpty() )
    9. {
    10. QSvgGenerator generator;
    11. generator.setFileName(fileName);
    12. generator.setSize(QSize(1024, 800));
    13. int options = QwtPlotPrintFilter::PrintAll;
    14. options |= QwtPlotPrintFilter::PrintBackground;
    15. options |= QwtPlotPrintFilter::PrintFrameWithScales| QwtPlotPrintFilter::PrintMargin;
    16. filter.setOptions(options);
    17. QPixmap pixmap = QPixmap::grabWidget(this);
    18. QPainter painter(&generator);
    19. painter.drawPixmap(0,0,-1,-1,pixmap);
    20.  
    21.  
    22.  
    23. // print(&painter,QRect(0,0,800,600),filter);:confused:
    24.  
    25.  
    26. }
    27.  
    28.  
    29. }
    30. void MyPlot::exportPNG()
    31. { QString fileName;
    32.  
    33. fileName = QFileDialog::getSaveFileName(this, tr("Nome file da esportare"), QString(),"Graphic files (*.png )");
    34.  
    35. if ( !fileName.isEmpty() )
    36. {
    37. // int options = QwtPlotPrintFilter::PrintAll;
    38. // int options = ~QwtPlotPrintFilter::PrintBackground;
    39. int options = QwtPlotPrintFilter::PrintFrameWithScales;
    40. options |= QwtPlotPrintFilter::PrintBackground;
    41. filter.setOptions(options);
    42.  
    43. QPixmap pixmap= QPixmap::grabWidget(this);
    44.  
    45.  
    46. //print(pixmap,filter );:confused:
    47.  
    48. if ( pixmap.save(fileName, "png" ))
    49. qDebug()<<"ok";
    50. else
    51. qDebug()<<"Uhmm";
    52. }
    53. }
    To copy to clipboard, switch view to plain text mode 

  11. #9
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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:rint in the bode example with the following code:

    Qt Code:
    1. void MainWin::print()
    2. {
    3. QPixmap pixmap(600, 600);
    4. pixmap.fill(Qt::white); // Qt::transparent ?
    5.  
    6. int options = QwtPlotPrintFilter::PrintAll;
    7. options &= ~QwtPlotPrintFilter::PrintBackground;
    8. options |= QwtPlotPrintFilter::PrintFrameWithScales;
    9. filter.setOptions(options);
    10.  
    11. d_plot->print(pixmap, filter);
    12.  
    13. QLabel *label = new QLabel(NULL);
    14. label->setPixmap(pixmap);
    15. label->show();
    16. }
    To copy to clipboard, switch view to plain text mode 

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

    Uwe

  12. #10
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Saving qwt plot as image

    Quote Originally Posted by Uwe View Post
    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

  13. #11
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Saving qwt plot as image-QwtPlot::print() bug

    Quote Originally Posted by Uwe View Post

    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....

  14. #12
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Saving qwt plot as image-QwtPlot::print() bug

    Try Qwt from SVN ( 5.1 branch ).

    Uwe

  15. The following user says thank you to Uwe for this useful post:

    giusepped (22nd December 2008)

  16. #13
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Saving qwt plot as image

    Great! Now all works!
    Thank you.

  17. #14
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Saving qwt plot as image-QwtPlot::print() bug

    Quote Originally Posted by giusepped View Post
    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.

    Qt Code:
    1. void MyPlot::exportSVG()
    2. {
    3. QString fileName;
    4.  
    5.  
    6. fileName = QFileDialog::getSaveFileName(this, tr("Nome file da esportare"), QString(),"Graphic files ( *svg)");
    7.  
    8. if ( !fileName.isEmpty() )
    9. {
    10. QSvgGenerator generator;
    11. generator.setFileName(fileName);
    12. generator.setSize(QSize(1024, 800));
    13. int options = QwtPlotPrintFilter::PrintAll;
    14. options |= QwtPlotPrintFilter::PrintBackground;
    15. options |= QwtPlotPrintFilter::PrintFrameWithScales| QwtPlotPrintFilter::PrintMargin;
    16. filter.setOptions(options);
    17. QPixmap pixmap = QPixmap::grabWidget(this);
    18. QPainter painter(&generator);
    19. painter.drawPixmap(0,0,-1,-1,pixmap);
    20.  
    21.  
    22.  
    23. // print(&painter,QRect(0,0,800,600),filter);:confused:
    24.  
    25.  
    26. }
    27.  
    28.  
    29. }
    30. void MyPlot::exportPNG()
    31. { QString fileName;
    32.  
    33. fileName = QFileDialog::getSaveFileName(this, tr("Nome file da esportare"), QString(),"Graphic files (*.png )");
    34.  
    35. if ( !fileName.isEmpty() )
    36. {
    37. // int options = QwtPlotPrintFilter::PrintAll;
    38. // int options = ~QwtPlotPrintFilter::PrintBackground;
    39. int options = QwtPlotPrintFilter::PrintFrameWithScales;
    40. options |= QwtPlotPrintFilter::PrintBackground;
    41. filter.setOptions(options);
    42.  
    43. QPixmap pixmap= QPixmap::grabWidget(this);
    44.  
    45.  
    46. //print(pixmap,filter );:confused:
    47.  
    48. if ( pixmap.save(fileName, "png" ))
    49. qDebug()<<"ok";
    50. else
    51. qDebug()<<"Uhmm";
    52. }
    53. }
    To copy to clipboard, switch view to plain text mode 



    thanks for this post

Similar Threads

  1. export and printing qwt plot
    By giusepped in forum Qwt
    Replies: 6
    Last Post: 17th December 2008, 07:04
  2. Problem saving a plot
    By kalos80 in forum Qwt
    Replies: 2
    Last Post: 10th July 2008, 08:31
  3. Qwt plot problem on compile
    By sincnarf in forum Qwt
    Replies: 2
    Last Post: 14th October 2007, 11:36
  4. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10
  5. Saving already opened image ...
    By Godlike in forum Newbie
    Replies: 14
    Last Post: 28th March 2006, 21:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.