一、前言
项目做多了,会发现有很多功能是必须经历的,这就是所谓的不是不报时辰未到
二、环境
qt5.7mingw
windows8
三、正文
在pro文件中必须添加printsupport这个库
#include <QPrinter>
#include <QPainter>
connect(ui->paintPDF,&QPushButton::clicked,[=](){
QPrinter printer;
QString desktop_path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
//QString str = QFileDialog::getSaveFileName(this,tr("Save Text"),"/",tr("Text Files (*.pdf)"));
printer.setOutputFormat(QPrinter::PdfFormat);//设置输出为pdf
printer.setPageSize(QPrinter::A4);//设置A4大小
printer.setOrientation(QPrinter::Landscape);//纵向Portrait 横向Landscape
printer.setOutputFileName(desktop_path+"/"+QDateTime::currentDateTime().toString("yyyyMMddHHmmss")+".pdf");//设置路径和名称
ui->textEdit->print(&printer);//输出文本
QMessageBox::information(0,QObject::tr("提示"),QObject::tr("文件存储在桌面:%1").arg(QDateTime::currentDateTime().toString("yyyyMMddHHmmss")+".pdf"));
});
connect(ui->paintView,&QPushButton::clicked,[=](){
QPrinter printer;
QPrintPreviewDialog previewDialog(&printer,this);//QPrintDialog类直接打印不预览
connect(&previewDialog,SIGNAL(paintRequested(QPrinter*)),this,SLOT(Preview(QPrinter*) ) );
previewDialog.exec(); //打印对话框显示,paintRequest触发
});
void MainWindow::Preview(QPrinter *printer)
{//printer设置打印属性 //painter设置内容属性
QPainter painter(printer);
printer->setPageSize(QPrinter::A4);//设置A4大小
printer->setOrientation(QPrinter::Portrait);//纵向:Portrait 横向:Landscape
//HTML方式画表格
QString course[7];course[3]="sdg的就是高考路上的睡到几点\nsdgs的进口关税的";
uint grade[7],xuefen[7],jidian[7];
//已知HTML格式
QString strStream;
QTextStream out(&strStream);
out<< "<table border='1' cellpadding='10'>"
" <tr> <td>编号</td> <td>课程名称</td> <td>成绩</td> <td>学分</td> <td>绩点</td> </tr> "
" <tr> <td> 1 </td> <td>"+course[0]+"</td> <td>"+grade[0]+"</td> <td>"+xuefen[0]+"</td> <td>"+jidian[0]+"</td> </tr> "
" <tr> <td> 2 </td> <td>"+course[1]+"</td> <td>"+grade[1]+"</td> <td>"+xuefen[1]+"</td> <td>"+jidian[1]+"</td> </tr> "
" <tr> <td> 3 </td> <td>"+course[2]+"</td> <td>"+grade[2]+"</td> <td>"+xuefen[2]+"</td> <td>"+jidian[2]+"</td> </tr> "
" <tr> <td> 4 </td> <td>"+course[3]+"</td> <td>"+grade[3]+"</td> <td>"+xuefen[3]+"</td> <td>"+jidian[3]+"</td> </tr> "
" <tr> <td> 5 </td> <td>"+course[4]+"</td> <td>"+grade[4]+"</td> <td>"+xuefen[4]+"</td> <td>"+jidian[4]+"</td> </tr> "
" <tr> <td> 6 </td> <td>"+course[5]+"</td> <td>"+grade[5]+"</td> <td>"+xuefen[5]+"</td> <td>"+jidian[5]+"</td> </tr> "
" <tr> <td> 7 </td> <td>"+course[6]+"</td> <td>"+grade[6]+"</td> <td>"+xuefen[6]+"</td> <td>"+jidian[6]+"</td> </tr> "
"</table> ";
//ui->textEdit->insertHtml(strStream);//将表格显示在textedit上面
QTextDocument*document=new QTextDocument;
document->setHtml(strStream);
document->drawContents(&painter);
//打印界面或者指定控件
QPixmap image;
image=image.grabWidget(ui->w_ybp2,0,0,ui->w_ybp2->width(),ui->w_ybp2->height());
// QRect rect = painter.viewport();//设置空间最宽,占满表格宽度
// QSize size = image.size();
// size.scale(rect.size(), Qt::KeepAspectRatio); //此处保证图片显示完整
// painter.setViewport(rect.x(), rect.y(),size.width(), size.height());
// painter.setWindow(image.rect());
painter.drawPixmap(0,0,image);
setpainterfont(painter,80,"宋体",true,false,true);//设置打印文字大小,字体,斜体,粗体,下划线
painter.drawText(50,600,700,80,Qt::AlignCenter,"沈阳sy科技");
printer->newPage();//下一页
setpainterfont(painter,16,"宋体",false,false,false);//设置打印文字大小,字体,斜体,粗体,下划线
painter.drawText(30,20,"fsufhsduifds圣诞节犯得上看见");
painter.restore();
}
//设置打印文字大小,字体,斜体,粗体,下划线
void MainWindow::setpainterfont(QPainter &paint,int size,QString word,bool ita,bool blod,bool under)
{
QFont font;
font.setPointSize(size);
font.setFamily(word);
font.setItalic(ita);//斜体
font.setBold(blod);//粗体
font.setUnderline(under);//下划线
paint.setFont(font);
}
然后按照如上的方式随便建立两个按键测试即可明白怎么使用。
主要是框架部分,实际内容还需要自己paint