我正在尝试在单独的页面中呈现每个 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
我试图用谷歌搜索这些消息,但它们都与渲染 qwidgets 无关。任何帮助表示赞赏。谢谢你。