Results 1 to 6 of 6

Thread: How to save a plot as .jpg..

  1. #1
    Join Date
    Sep 2009
    Location
    Surrey, BC, Canada
    Posts
    110
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question How to save a plot as .jpg..

    Hi, how to save a plot (created by QWT) to a .jpg file?

    Cheers
    JIA
    Welcome to Vision Open
    http://www.visionopen.com

  2. #2
    Join Date
    Oct 2008
    Location
    Aachen
    Posts
    20
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: How to save a plot as .jpg..

    Take a look at QPixmap and try somethihng like this:
    Qt Code:
    1. void Plot::saveAS()
    2. {
    3. QPixmap pixmap(this->size());
    4. pixmap.fill(Qt::transparent);
    5.  
    6. int options = QwtPlotPrintFilter::PrintAll;
    7. // options &= ~QwtPlotPrintFilter::PrintBackground;
    8. // options &= ~QwtPlotPrintFilter::PrintGrid;
    9. filter.setOptions(options);
    10.  
    11. if(!pixmap.isNull()){
    12. QString fileName = QFileDialog::getSaveFileName(this, tr("Save the file as..."), QDir::currentPath(), tr("JPEG (*.jpg);;Portable Network Graphics (*.png)"));
    13. if (!fileName.isEmpty()){
    14. if(fileName.endsWith(".png")){
    15. this->print(pixmap, filter);
    16. pixmap.save(fileName, "PNG");
    17. }
    18. else if(fileName.endsWith(".jpg")){
    19. this->print(pixmap, filter);
    20. pixmap.save(fileName, "JPG");
    21. }
    22. else{
    23. return;
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2009
    Location
    Surrey, BC, Canada
    Posts
    110
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Re: How to save a plot as .jpg..

    Thank you very much. It works.

    However, as attached,

    "shouldbe" is the plot that I should have obtained while
    "obtained" is the wrong figure that I finally drew.



    So, a further question:

    How can I specify "including" the plot's X Axis and Y Axis (particularly, those descriptions)?

    And, anything else I need to pay attention to?

    Best Regards
    JIA
    Attached Images Attached Images
    Welcome to Vision Open
    http://www.visionopen.com

  4. #4
    Join Date
    Sep 2009
    Location
    Surrey, BC, Canada
    Posts
    110
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Talking Re: How to save a plot as .jpg..

    Alright, I got it .

    Remove

    Qt Code:
    1. pixmap.fill(Qt::transparent);
    To copy to clipboard, switch view to plain text mode 

    Cheers
    JIA
    Welcome to Vision Open
    http://www.visionopen.com

  5. #5
    Join Date
    Oct 2009
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to save a plot as .jpg..

    My code is producing a JPEG similar to the one you labeled "obtained" above where the plot area is correct but it is black all around the border where there should be axis titles and chart titles. I removed the pixmap.fill(Qt::transparent) call, but still get the black border. What am I doing wrong?

    When I use PNG format the images turn out fine.

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

    Default Re: How to save a plot as .jpg..

    QwtPlot doesn't fill the areas behind axes, title and legend. If you don't initialize your image (better use QImage instead of QPixmap if you want to produce jpegs or pngs ) you will see random ( here black ) pixels. Do a "pixmap.fill();" to initialize it.

    And once more I recommend to use a scalable vector format like pdf or svg instead of rastered images.

    Uwe

Similar Threads

  1. Put plot axis out of the plot
    By KosyakOFF in forum Qwt
    Replies: 7
    Last Post: 21st June 2013, 13:36
  2. Spectrogram Plot Hints
    By shargath in forum Qwt
    Replies: 2
    Last Post: 25th February 2009, 11:11
  3. Replies: 11
    Last Post: 31st December 2008, 12:30
  4. Replies: 7
    Last Post: 22nd September 2008, 22:05
  5. What does "Save All" actually save?
    By Mariane in forum Newbie
    Replies: 7
    Last Post: 31st January 2006, 13:23

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.