santhosh
santhosh

Reputation: 1902

How to save a chart as an image in QT

I have created a chart in QT. Is there a way that I can save this as an Image?

Thanks,

Upvotes: 0

Views: 3679

Answers (1)

Arne
Arne

Reputation: 2674

When you say chart, I assume it is some kind of vector graphics or similar. I usually export them as PDF using QPrinter and then use them in my documents (LaTeX, Keynote, ...). Here is a code snippet which might help. Untested, since I ported it in my head from Python back to C++:

  QPrinter *printer = new QPrinter();
  printer->setOrientation(QPrinter::Landscape);
  QPrintDialog *dialog = new QPrintDialog(printer);
  dialog->setWindowTitle("Print Plots");
  if (dialog.exec() != QDialog::Accepted)
     return;
  QPainter *painter = new QPainter();
  painter->begin(printer);
  ui->someQwtPlot->print(painter, printer->pageRect());
  painter->end();
  delete(dialog);
  delete(painter);
  delete(printer);

I am assuming here you are creating a plot with QwtPlot, but any QWidget or object supporting rendering to a QPainter will do. See QWidget::render() for details. The code above actually allows you even to print to a real printer. But I usually just set "Print to PDF" in the printer dialog. If you do not need the printer dialog, you can skip it and use QPrinter::setOutputFilename to set the PDF filename directly.

Upvotes: 5

Related Questions