Results 1 to 2 of 2

Thread: Save QChartView as PNG

  1. #1
    Join Date
    May 2009
    Posts
    52
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Save QChartView as PNG

    I want to save a chart as PNG. I'm doing this:

    Qt Code:
    1. QChartView* chartView = new QChartView;
    2. chartView->setRenderHint(QPainter::Antialiasing);
    3. chartView->resize(printer.width(), printer.width() * 0.60);
    4.  
    5. QChart* chart = new QChart;
    6. chart->legend()->hide();
    7. chartView->setChart(chart);
    8.  
    9. QDateTimeAxis* xAxis = new QDateTimeAxis;
    10. xAxis->setFormat("dd.MM.yyyy");
    11. chart->addAxis(xAxis, Qt::AlignBottom);
    12.  
    13. QValueAxis* yAxis = new QValueAxis;
    14. chart->addAxis(yAxis, Qt::AlignLeft);
    15.  
    16. QLineSeries* series = new QLineSeries;
    17. series->append(data);
    18. series->setPointsVisible(true);
    19.  
    20. chart->addSeries(series);
    21. series->attachAxis(xAxis);
    22. series->attachAxis(yAxis);
    23.  
    24. QPixmap p = chartView->grab();
    25. p.save("a.png", "PNG");
    To copy to clipboard, switch view to plain text mode 

    The problem is that the axes are not shown. If I show the widget the axes are visible, but even then they are not saved in the PNG...

    Any idea how to make them appear in the image?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,974
    Thanks
    277
    Thanked 808 Times in 797 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Save QChartView as PNG

    Try using QWidget::render() instead of QWidget::grab():

    Qt Code:
    1. QPixmap p( chartView->size() );
    2. chartView->render( &p );
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. adding new QChart in a QChartView
    By zemlemer in forum Newbie
    Replies: 3
    Last Post: 7th September 2019, 16:02
  2. Problem getting QChartView to display
    By derrickbj in forum Qt Programming
    Replies: 5
    Last Post: 20th September 2016, 16:55
  3. How to do QChartView delegate like QTableView item delegate
    By malleeswarareddy.s in forum Qt Programming
    Replies: 0
    Last Post: 16th September 2015, 11:37
  4. QVariant : save : unable to save type 128
    By capbee in forum Qt Programming
    Replies: 0
    Last Post: 15th July 2013, 09:10
  5. Replies: 4
    Last Post: 29th May 2012, 23:37

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.