Qt打印主要有3种方式:
1、直接调用qpainter接口,绘出自己想要的结果
2、采用HTML输出自己想要的内容
3、调用scene->render(painter,QRectF(),scene->itemsBoundingRect());得出结果
接下来具体说说怎么实现、其中的原理和注意事项。
方式一:调用qpainter接口
关键代码如下:
// 获取纸张大小
QSizeF
pageSize(printer->pageRect().width(),printer->pageRect().height());
// 调用文字接口drawText在纸张上下中心靠左的位置打印“需要显示的文字”
painter->drawText(QPointF(0,pageSize.height()/2),QString("%1").arg(“需要显示的文字));
// 关闭画图工具
painter->restore();
附:绘图接口qpainter.h在Qt源代码路径include\QtGui\qpainter.h下
比如:
直线:
voidQPainter::drawLine(intx1,inty1,intx2,inty2);
折线:
voidQPainter::drawPolyline(constQPolygonF&polyline);
圆 :
void
drawEllipse(constQRectF&r);
void
drawEllipse(constQ