开发者

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 Im开发者_Python百科age?

Thanks,


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜