如何使用 Qpainter 将多个 qwidgets 打印到不同页面中的 pdf?
How to print multiple qwidgets to a pdf in different pages using Qpainter?
我正在尝试在单独的页面中呈现每个 qwidgets 并将其保存为 pdf 格式。我尝试使用 Qpainter、Qprinter 并尝试按照论坛中的建议进行渲染:http://doc.qt.io/qt-4.8/printing.html#printing-widgets
这是我的示例代码:
QPainter painter;
painter.begin(&printer);
for(int page = 0;page<no_of_pages;page++)
{
double xscale = printer.pageRect().width()/double(MyWidget[page]->width());
double yscale = printer.pageRect().height()/double(MyWidget[page]->height());
double scale = qMin(xscale, yscale);
painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
printer.paperRect().y() + printer.pageRect().height()/2);
painter.scale(scale, scale);
painter.translate(-MyWidget[page]->width()/2, -MyWidget[page]->height()/2);
MyWidget[page]->render(&painter);
if(page!=(no_of_pages-1))
printer.newPage();
}
painter.end();
当我执行上面的代码时,生成的 pdf 中包含准确的页数,但只打印了第一页。当我检查应用程序输出控制台时,打印了以下消息:
QPainter::begin: Paint device returned engine == 0, type: 2
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setWorldTransform: Painter not active
QWidget::render: Cannot render with an inactive painter
QPainter::end: Painter not active, aborted
我试图 google 这些消息,但其中 none 与呈现 qwidgets 相关。任何帮助表示赞赏。谢谢。
问题不是它没有打印到下一页,而是比例不正确,您必须在每次迭代结束时使用 resetTransform()
重置所有转换。我更喜欢将小部件保存在 QList <QWidget *>
.
中
QList <QWidget *> widgets;
widgets.append({some widget});
[...]
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("printer.pdf");
QPainter painter;
if (! painter.begin(&printer)) { // failed to open file
qWarning("failed to open file, is it writable?");
}
for(auto widget: w){
if(widget){
if(widget->width() > 0 && widget->height() >0 ){
qreal xscale = 0.9*printer.pageRect().width()/qreal(widget->width());
qreal yscale = 0.9*printer.pageRect().height()/qreal(widget->height());
qreal scale = qMin(xscale, yscale);
painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
printer.paperRect().y() + printer.pageRect().height()/2);
painter.scale(scale, scale);
painter.translate(-width()/2, -height()/2);
widget->render(&painter);
painter.resetTransform();
if(widget != widgets.last())
printer.newPage();
}
}
}
painter.end();
观察:设置系数0.9,让图片显示的更舒服。
我正在尝试在单独的页面中呈现每个 qwidgets 并将其保存为 pdf 格式。我尝试使用 Qpainter、Qprinter 并尝试按照论坛中的建议进行渲染:http://doc.qt.io/qt-4.8/printing.html#printing-widgets
这是我的示例代码:
QPainter painter;
painter.begin(&printer);
for(int page = 0;page<no_of_pages;page++)
{
double xscale = printer.pageRect().width()/double(MyWidget[page]->width());
double yscale = printer.pageRect().height()/double(MyWidget[page]->height());
double scale = qMin(xscale, yscale);
painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
printer.paperRect().y() + printer.pageRect().height()/2);
painter.scale(scale, scale);
painter.translate(-MyWidget[page]->width()/2, -MyWidget[page]->height()/2);
MyWidget[page]->render(&painter);
if(page!=(no_of_pages-1))
printer.newPage();
}
painter.end();
当我执行上面的代码时,生成的 pdf 中包含准确的页数,但只打印了第一页。当我检查应用程序输出控制台时,打印了以下消息:
QPainter::begin: Paint device returned engine == 0, type: 2
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setWorldTransform: Painter not active
QWidget::render: Cannot render with an inactive painter
QPainter::end: Painter not active, aborted
我试图 google 这些消息,但其中 none 与呈现 qwidgets 相关。任何帮助表示赞赏。谢谢。
问题不是它没有打印到下一页,而是比例不正确,您必须在每次迭代结束时使用 resetTransform()
重置所有转换。我更喜欢将小部件保存在 QList <QWidget *>
.
QList <QWidget *> widgets;
widgets.append({some widget});
[...]
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("printer.pdf");
QPainter painter;
if (! painter.begin(&printer)) { // failed to open file
qWarning("failed to open file, is it writable?");
}
for(auto widget: w){
if(widget){
if(widget->width() > 0 && widget->height() >0 ){
qreal xscale = 0.9*printer.pageRect().width()/qreal(widget->width());
qreal yscale = 0.9*printer.pageRect().height()/qreal(widget->height());
qreal scale = qMin(xscale, yscale);
painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
printer.paperRect().y() + printer.pageRect().height()/2);
painter.scale(scale, scale);
painter.translate(-width()/2, -height()/2);
widget->render(&painter);
painter.resetTransform();
if(widget != widgets.last())
printer.newPage();
}
}
}
painter.end();
观察:设置系数0.9,让图片显示的更舒服。