Qt-打印文件
作者:互联网
QPrintDialog是Qt提供的标准打印窗口
QPrintDialog标准对话框提供了打印机的选择、配置功能,且允许用户修改文档有关的设置,如页面范围,打印份数等
1、文本打印:
1 QPrinter printer; 2 QPrinterDialog printDlg(&printer, this); 3 if(printDlg.exec()) 4 { 5 QTextDocument* doc = textEdit->document(); 6 doc->print(&printer); 7 }
2、图像打印:
1 QPrinter printer; 2 QPrinterDialog printDlg(&printer, this); 3 if(printDlg.exec()) 4 { 5 QPainter painter(&printer); 6 QRect rect = painter.vewport(); //获取QPainter对象的视图矩形区域 7 QSize size = img.size(); //这是一个Label 8 size.scale(rect.size(), Qt::KeepAspectRation); 9 painter.setViewport(rect.x(), rect.y(), size.width(), size.height()); //重新设置矩形区域 10 painter.setWindow(img.rect()); //设置QPainter窗口大小为图像的大小 11 painter.drawImage(0,0,img); 12 }
标签:printer,Qt,打印文件,QPainter,printDlg,painter,rect,size 来源: https://www.cnblogs.com/fengye2021/p/15363387.html