截图指定区域图片并导出至PDF文件

导出效果:
截图指定区域图片并导出至PDF文件_第1张图片
截图指定区域图片并导出至PDF文件_第2张图片

void QTabTreatVerifyPanel::ExportShotScreenSlot(QString path)
{
//m_pixmap = QPixmap::grabWindow(winId(), 338, 460, 652, 455);//截取界面指定区域图像

/*此种方法有可能会导致软件界面上部分控件被隐藏*/
//QScreen* screen = QGuiApplication::primaryScreen();
//m_pixmap = screen->grabWindow(this->winId(), 338, 460, 652, 455);//截取界面指定区域图像
//ui.label->setPixmap(m_pixmap.scaled(ui.label->size()));//将所截图显示在label中

/*此种方法可解决控件隐藏问题*/
m_pixmap = this->grab(QRect(338, 460, 652, 455));

QDateTime dateTime = QDateTime::currentDateTime();				//当前日期和时间
QString ExportTime = dateTime.toString("yyyyMMddhhmmsszzz");	//导出时间
/***************************************************************/
QString pdfPath = path + ExportTime + "ExoprtShotScreenFile.pdf";//导出路径
	
QPrinter printer_html;
printer_html.setPageSize(QPrinter::A4);
printer_html.setOutputFormat(QPrinter::PdfFormat);
printer_html.setOutputFileName(pdfPath);

QPainter painter_pixmap;
painter_pixmap.begin(&printer_html);
/*QRect rect = painter_pixmap.viewport();
int multiple = rect.width() / m_pixmap.width();
qDebug() << "mul = " << multiple;*/
//painter_pixmap.scale(multiple, multiple + 1); // 缩放比例


painter_pixmap.setPen(Qt::black);
painter_pixmap.setFont(QFont("Arial", 30));
painter_pixmap.drawText(QRect(60,70,652,50), Qt::AlignCenter, QStringLiteral("温度图数据"));
painter_pixmap.drawPixmap(60, 150, m_pixmap);//绘制截图

printer_html.newPage();
painter_pixmap.resetTransform();
QString sTime2 = QStringLiteral("序号") + "        " + QStringLiteral("图像时间") + "        " + QStringLiteral("焦点温度") + "        " + QStringLiteral("头皮温度");

painter_pixmap.setPen(Qt::black);
painter_pixmap.setFont(QFont("Arial", 10));
painter_pixmap.drawText(QRect(60, 70, 652, 20), Qt::LeftToRight, sTime2);


painter_pixmap.setFont(QFont("Arial", 8));
int count = 0;

for (int i = 0; i < m_vCurveDataSkin.size(); i++)
{
	count++;
	if (count % 64 == 0)
	{
		printer_html.newPage();
		painter_pixmap.resetTransform();
	}
	QString sTime3 = QString::number(count) + "                " + m_vAcqTimes.at(i).left(8) + "                " + QString::number(m_vCurveData.at(i), 'f', 2) + QStringLiteral("℃") + "                " + QString::number(m_vCurveDataSkin.at(i), 'f', 2) + QStringLiteral("℃");
	painter_pixmap.drawText(QRect(60, 80 + (count % 64) * 15, 652, 15), Qt::LeftToRight, sTime3);
}

painter_pixmap.end();

m_pDlgTemperatureCurveSet->show();
//QDesktopServices::openUrl(QUrl::fromLocalFile(pdfPath));//显示PDF文件	

}

你可能感兴趣的:(Qt,pdf,截屏,Qt导出pdf)