qq音乐怎么做mp3下载网站/四川游戏seo整站优化
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(constQRect&r);
void
drawEllipse(intx,inty,intw,inth);
方式二:HTML方式
关键代码如下:
QString strStream;
QTextStream out(&strStream);
// 下面是HTML内容
数据通过QTextStream存储到QString(strStream)
// HTML内容结束
QTextDocument*document=newQTextDocument();
document->setHtml(strStream);
document.drawContents(painter);
注意:写HTML文件时,需要留意一下“;”,不要缺少,也不要增加。
方式三:scene->render
//QGraphicsView
和QGraphicsScene变量初始化
QGraphicsView *view
=newQGraphicsView();
QGraphicsScene *scene=newIcdMdlScene(view);
view->setScene(scene);
// 对QGraphicsScene画图(自定义内容)
scene->drawContent();
// 将QGraphicsScene的内容画到纸上
scene->render(painter,QRectF(),scene->itemsBoundingRect());
总结:
对于简单模型建议使用方式一(调用qpainter接口)直接画出
对于表格数据建议使用方式二(HTML方式);
对于图片展示,内容丰富建议使用方式三(scene->render);