如何解决在多页上打印QGraphicsScene
我有这样的代码:
QPrinter printer;
if (QPrintDialog(&printer).exec() == QDialog::Accepted) {
QPainter painter(&printer);
scene->render(&painter,QRectF(0,10000,10000),1024,1024),Qt::KeepAspectRatioByExpanding);
}
我希望场景的内容将被缩放并打印在多页上。缩放效果很好,但是pdf文件(我使用pdf打印机)仅包含一页:场景的左上角缩放。
Qt 5.15。项目文件中的Qt + = printsupport选项。
解决方法
对于多页打印,您必须自己管理。
这是我使用的代码:
void printScene(QGraphicsScene* scene,qreal pageHeight,QPrinter& printer,int pageCount)
{
if (scene)
{
if (pageCount < 1)
{
LOG_WARNING() << QString("invalid page count: %1. Defaulting to 1").arg(pageCount);
pageCount = 1;
}
QRectF pageRect(0,scene->sceneRect().width(),pageHeight);
qreal paperHeight = scene->sceneRect().height();
QPainter painter(&printer);
for (int page = 0; page < pageCount; ++page)
{
scene->render(&painter,QRectF(),pageRect);
paperHeight -= pageHeight;
pageRect.translate(0,pageHeight);
pageRect.setHeight(qMin(pageHeight,paperHeight));
if ((page + 1) < pageCount)
printer.newPage();
}
}
else
LOG_ERROR() << ("attempt to print a null scene");
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。