Qt之导出PDF、HTML和Word(一)

来源:互联网 发布:网络施工系统报价清单 编辑:程序博客网 时间:2024/06/05 07:40
内容提要:
1,Qt导出文件的基本原理;
2,QPrinter、HTML和PDF;
3,HTML编辑器;
4,HTML和Word;
5,qwt的图片和pdf导出

一、Qt绘图和导出文件的基本原理
Qt的二维图形引擎是基于QPainter类的。QPainter既可以绘制几何形状(点、线、矩形、椭圆、弧形、弦行、饼状图、多边形和贝塞尔曲线),也可以绘制像素映射、图像和文字。(参考《C++GUI Programming with Qt》)
查看Qt的帮助文档,关于“QPainter”的描述:
The QPainter class performs low-level painting on widgets andother paint devices.
QPainter provides highly optimized functions to do most of thedrawing GUI programs require. It can draw everything from simplelines to complex shapes like pies and chords. It can also drawaligned text and pixmaps. Normally, it draws in a "natural"coordinate system, but it can also do view and worldtransformation. QPainter can operate on any object that inheritsthe QPaintDevice class.
The common use of QPainter is inside a widget's paint event:Construct and customize (e.g. set the pen or the brush) thepainter. Then draw. Remember to destroy the QPainter object afterdrawing. 
 QPainter可以在任何“绘图设备”(继承自QPaintDevice的类)上进行绘画,例如:QWidget、QPixmap、QImage、QSvgGenerator等。要想在绘图设备上绘图,只需创建一个QPainter对象,再将指针传到该设备中即可在该设备的paintEvent中进行绘画。
QPainter也可以与QPrinter一起使用来打印文件和创建PDF文档。查看Qt的帮助文档,关于“QPrinter”的描述如下:
The QPrinter class is a paint device that paints on aprinter.
This device represents a series of pages of printed output,and is used in almost exactly the same way as other paint devicessuch as QWidget and QPixmap. A set of additional functions areprovided to manage device-specific features, such as orientationand resolution, and to step through the pages in a document as itis generated.
QPrinter本质上也是一个绘图设备,在QPrinter上绘图和在其他设备上绘图的方法非常类似,不同的是一些设备相关的管理函数。(参考《C++GUI Programming with Qt》和 Qt之生成PDF)QPrinter可以设置两种输出格式:QPrinter::NativeFormat和QPrinter::PdfFormat,前一种是直接打印,后一种是导出为pdf文档。

二、QPrinter导出PDF
注:要使用QPrinter,需在.pro文件中“QT +=printsupport”。
1,导出图片(参考博文“Qt之生成PDF”)
QPrinter printer_pixmap(QPrinter::HighResolution);
printer_pixmap.setPageSize(QPrinter::A4); //设置纸张大小为A4
printer_pixmap.setOutputFormat(QPrinter::PdfFormat); //设置输出格式为pdf
printer_pixmap.setOutputFileName("E:\\test_pixmap.pdf");  //设置输出路径
QPixmap pixmap = QPixmap::grabWidget(main_widget,main_widget->rect());  //获取界面的图片

QPainter painter_pixmap;   // 使用QPainter在绘图设备上绘制
painter_pixmap.begin(&printer_pixmap);
QRect rect = painter_pixmap.viewport();
int multiple = rect.width()/pixmap.width();
painter_pixmap.scale(multiple, multiple);//将图像(所有要画的东西)在pdf上放大multiple-1倍
painter_pixmap.drawPixmap(0, 0, pixmap);  //调用QPainter的对应函数画图
painter_pixmap.end();

2,生成文本(参考博文“Qt之生成PDF”)
不同的是调用QPainter的drawText()函数。此外,需要调用QPrinter的newPage()函数去手动分页。

3,生成HTML(参考博文“Qt之生成PDF”)
对于单页的文档,可以采用以上两种方法,对于多页的复杂的文档,为免分页等麻烦,一采用HTML转PDF的方式。以下给出一个简单示例。
使用QCreator新建console工程,首先在.pro文件中添加“QT += printsupport”;然后按照博文Qt之生成PDF修改main文件。
主要过程是:
首先编辑HTML并保存到QString中,再利用Qt的富文本引擎QTextDocument支持“richtext”的特性,将该QString设置为html格式(保留html的特性),最后用QTextDocument关联QPrinter。

三、QFile+QString直接导出HTML文件
参考QWT的examples中的“stockchart”,其中可以直接导出界面为html。关键代码如下:
voidPlot::report()
{
    QString strInfor;
    QString strDataDirectory= "Data";
    QStringpath(QDir::currentPath());
    QDirdir(QDir::currentPath());
    if(!dir.cd(strDataDirectory))  // 判断Data目录是否存在
    {
       strInfor = strDataDirectory + tr(" is notexist!", "need translate");
       return;
    }

    QStringstrReportDirectoryName =QDateTime::currentDateTime().toString("yyyy-MM-dd_hh-mm-ss");//获取系统当前时间并设置格式
   dir.mkdir(strReportDirectoryName);
    if(!dir.cd(strReportDirectoryName))  //判断Report目录是否存在
    {
       strInfor = strDataDirectory + "\\" +strReportDirectoryName + tr(" is not exist!", "needtranslate");;
       return;
    }

    QStringstrImageDirectoryName = "Images";
   dir.mkdir(strImageDirectoryName);
    if(!dir.cd(strImageDirectoryName))  //判断Report中的Image目录是否存在
    {
       strInfor = strDataDirectory + "\\" +strReportDirectoryName + "\\" + strImageDirectoryName + tr(" is notexist!", "need translate");;
       return;
    }

    QString strImagePath =strDataDirectory + "\\" + strReportDirectoryName + "\\" +strImageDirectoryName;
    QStringstrImangeFileName = strImagePath + "\\" + "stockchart.bmp";
    QSizeF tmp(300,200);
    QwtPlotRendererrenderer;
   renderer.renderDocument(this, strImangeFileName, tmp);
   //renderer.renderDocument(this, "./Debug/stockchart.bmp",tmp);

    QStringstrHtmlReportPath = strDataDirectory + "\\" +strReportDirectoryName;
    QStringstrHtmlReportFileName = strHtmlReportPath + "\\" +"Report.html";

    QFilereport(strHtmlReportFileName);
   report.open(QIODevice::WriteOnly | QIODevice::Truncate);
    QTextStreamofs(&report);

    // 写入头数据到HTML文件
    QString strHeadHtml ="\n\n\n";
    strHeadHtml +="\n";
    strHeadHtml +=GetReturnTopScript();
    strHeadHtml +="\n\n";
    ofs <<strHeadHtml;

    //写入配置和图像数据到HTML文件
    QStringstrImageHtml;
strImageHtml += "

" + QObject::tr("Picture", "need translate") \
       + ": " + "stockchart" + "

\n";
    strImageHtml+= "

Qt之导出PDF、HTML和Word(一)
                   + "stockchart" + ".bmp\" title=\"" + "stockchart" + "\" alt=\"" +"stockchart" + "\" />

\n";
    strImageHtml+= "

 

";
    ofs <<strImageHtml;

    // 写入尾部数据到HTML文件
    ofs <<QString("\n");

    report.close();
}
注:它主要应用QString的拼接方式,将各个变量和图片链接导出到HTML文件中。

四、HTML编辑器
在工程中,不管是导出PDF还是导出HTML,其核心内容都是用HTML文件编写的。因此在这里介绍一个快速和方便编辑HTML的方法。
1,下载和安装一个“DreamWeaver”。
2,打开“DreamWeaver”,创建一个“html”项目。
Qt之导出PDF、HTML和Word(一)

可以在“设计模式”进行可视化设计,然后切换到“代码模式”拷贝代码,转换为QString即可。也可以反向操作,将代码拷贝到“代码模式”,再切换到“设计模式”进行编辑。

3,可以直接拖拽控件,然后在下方的属性中进行操作即可。
Qt之导出PDF、HTML和Word(一)

4,从html到C++代码有一个简单的转换:
while (stream.atEnd() == 0) {
           QStringstrBuf = stream.readLine();
          strBuf.append("\\r\\n");
          strBuf.replace("\"", "\\\"");
          streamOut<<m_strPrefix<<"\""<<strBuf<<"\";"<<endl;
       }
主要是:为了阅读和书写方便,添加换行,以及对转义字符的识别。此外,前缀是“html +=”,主要是语句的右值。

0 0
原创粉丝点击