我已经在 QT 中创建了一个图表。有没有办法可以将其另存为图像?
谢谢,
最佳答案
当您说图表时,我假设它是某种 vector 图形或类似图形。我通常使用 QPrinter 将它们导出为 PDF,然后在我的文档(LaTeX、Keynote 等)中使用它们。 这是一个可能有帮助的代码片段。未经测试,因为我将它从 Python 移植回 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);
我在这里假设您正在使用 QwtPlot 创建绘图,但任何支持渲染到 QPainter 的 QWidget 或对象都可以。参见 QWidget::render()了解详情。上面的代码实际上允许您打印到真正的打印机。但我通常只是在打印机对话框中设置“打印为 PDF”。如果不需要打印机对话框,可以跳过它并使用 QPrinter::setOutputFilename直接设置 PDF 文件名。
关于c++ - QT中如何将图表保存为图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7127059/