【Qt】Qt 导出 PDF,Android和Qt截图功能实现

一:Qt导出PDF

这里我是将3个图片(QPixmap:截图而得)画在了导出的pdf里面,其中第1.2张图片在第一页,第3张图片在第2页

代码:

    QPrinter printer_pixmap(QPrinter::HighResolution);
    printer_pixmap.setPageSize(QPrinter::A4);  //设置纸张大小为A4
    printer_pixmap.setPrintRange(QPrinter::AllPages);
    printer_pixmap.setOutputFormat(QPrinter::PdfFormat);  //设置输出格式为pdf
    printer_pixmap.setOutputFileName(outfilepath);   //设置输出路径


    QPainter painter_pixmap;
    painter_pixmap.begin(&printer_pixmap);
    QRect rect = painter_pixmap.viewport();
    int multiple = rect.width()/pixmap1.width();
    painter_pixmap.scale(multiple*0.675, multiple*0.675); //将图像(所有要画的东西)在pdf上放大multiple-1倍
    painter_pixmap.drawPixmap(120, 0, pixmap1);  //画图1
    painter_pixmap.drawPixmap(120, 580, pixmap2);//画图2
    printer_pixmap.newPage();  //换页
    painter_pixmap.drawPixmap(120, 0, pixmap3);//画图3
    painter_pixmap.end();

二:比较一下Android和Qt 截屏功能的实现:


android截屏用的是:

void android.view. View.draw( Canvas canvas)

代码:
//将某个View写成图片(截图功能)
	public static void writeImage(File previewTempFile,View v){  
		try {  
			Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
		            Bitmap.Config.ARGB_8888);
		    Canvas canvas = new Canvas(bitmap);
		    v.draw(canvas);
		    
		    //将Bitmap保存为png图片  
		    if(previewTempFile.exists())
		    	previewTempFile.delete();
		    try{
		    	previewTempFile.createNewFile();
		    } catch (IOException e){
		    	e.printStackTrace();
		    }
            FileOutputStream out = new FileOutputStream(previewTempFile);  
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);  
            Log.e("done", "done"); 
              
        } catch (Exception e) {  
            // TODO: handle exception  
            e.printStackTrace();  
        }  
    }

QT截屏用的是:

QPixmap QPixmap::grabWidget ( QWidget *widget, const QRect & rectangle ) [static]


代码:

 pixmap3 = QPixmap::grabWidget(Tab2,290,0,660,720);
 pixmap3.save(bmppath3.append("_c.bmp"),"bmp");



你可能感兴趣的:(android,Qt导出PDF,QT截图)