Qt中txt文件输出为PDF格式

server/2025/3/6 14:08:49/

 main.cpp

    PdfReportGenerator pdfReportGenerator;// 加载中文字体if (QFontDatabase::addApplicationFont(":/new/prefix1/simsun.ttf") == -1) {QMessageBox::warning(nullptr, "警告", "无法加载中文字体");}// 解析日志文件QVector<LogEntry> entries;if (!pdfReportGenerator.parseLogFile("1.txt", entries)) {QMessageBox::critical(nullptr, "错误", "无法打开日志文件");return ;}// 生成PDF表格pdfReportGenerator.createPdfWithTable(entries);QMessageBox::information(nullptr, "完成", "PDF表格已生成: output_table.pdf");

 

字体文件放入资源路径,字体下载网站GitCode - 全球开发者的开源社区,开源代码托管平台

 类实现

.cpp文件

#include "PdfReportGenerator.h"bool PdfReportGenerator::parseLogFile(const QString &filename, QVector<LogEntry> &entries) {QFile file(filename);if (!file.open(QIODevice::ReadOnly)) return false;QTextStream in(&file);in.setCodec("UTF-8");in.readLine(); // 跳过标题行while (!in.atEnd()) {QString line = in.readLine().trimmed();if (line.isEmpty()) continue;QStringList parts = line.split("\t", Qt::SkipEmptyParts);if (parts.size() < 6) continue;LogEntry entry;entry.command = parts[0].trimmed();entry.frame = parts[1].trimmed();entry.success = parts[2].trimmed().toInt();entry.fail = parts[3].trimmed().toInt();entry.timeout = parts[4].trimmed();entry.duration = parts[5].replace("ms", "").trimmed().toInt();entry.gapPos = (parts.size() > 6) ? parts[6].trimmed() : "";entries.append(entry);}return true;
}void PdfReportGenerator::createPdfWithTable(const QVector<LogEntry> &entries) {// 创建文档对象QTextDocument doc;QTextCursor cursor(&doc);// 设置中文字体QFont font("SimSun", 10);doc.setDefaultFont(font);// 添加标题cursor.insertText("日志数据报表\n", QTextCharFormat());cursor.insertBlock();// 创建表格(列数=7,行数=数据行数+1)QTextTable *table = cursor.insertTable(entries.size() + 1, 7);// 设置表头QStringList headers = {"命令", "帧/时间/次数", "成功次数","失败次数", "是否超时", "消耗时间", "缝隙位置"};for (int i = 0; i < headers.size(); ++i) {QTextTableCell cell = table->cellAt(0, i);QTextCursor cellCursor = cell.firstCursorPosition();cellCursor.insertText(headers[i]);cell.format().setBackground(Qt::lightGray); // 表头背景色}// 填充数据for (int row = 0; row < entries.size(); ++row) {const LogEntry &entry = entries[row];QStringList rowData = {entry.command,entry.frame,QString::number(entry.success),QString::number(entry.fail),entry.timeout,QString::number(entry.duration) + "ms",entry.gapPos};for (int col = 0; col < rowData.size(); ++col) {QTextTableCell cell = table->cellAt(row + 1, col);QTextCursor cellCursor = cell.firstCursorPosition();cellCursor.insertText(rowData[col]);// 数字列右对齐if (col >= 2 && col <= 5) {QTextBlockFormat format;format.setAlignment(Qt::AlignRight);cellCursor.setBlockFormat(format);}}}// 设置表格样式QTextTableFormat tableFormat;tableFormat.setHeaderRowCount(1);tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);tableFormat.setBorder(1);table->setFormat(tableFormat);// 导出PDFQPrinter printer(QPrinter::HighResolution);printer.setOutputFormat(QPrinter::PdfFormat);printer.setOutputFileName("output_table.pdf");printer.setPageSize(QPageSize(QPageSize::A4));doc.print(&printer);
}

.h文件 

#ifndef PDFREPORTGENERATOR_H
#define PDFREPORTGENERATOR_H
#include <QApplication>
#include <QFile>
#include <QTextStream>
#include <QTextDocument>
#include <QTextTable>
#include <QPrinter>
#include <QFontDatabase>
#include <QPdfWriter>
#include <QPainter>
#include "qcustomplot.h"struct LogEntry {QString command;QString frame;int success;int fail;QString timeout;int duration;QString gapPos;
};class PdfReportGenerator : public QObject {
public:void createPdfWithTable(const QVector<LogEntry> &entries);bool parseLogFile(const QString &filename, QVector<LogEntry> &entries);
};#endif // PDFREPORTGENERATOR_H

实现效果


http://www.ppmy.cn/server/172886.html

相关文章

Rust语言入门与应用:未来发展趋势解析

一、Rust语言核心优势解析 1.1 内存安全革命 rust复制 // 所有权系统示例 fn main() { let s1 String::from("hello"); // s1获得所有权 let s2 s1; // 所有权转移至s2 // println!("{}", s1); // 编译错误&#xff01;s1已失效 println!("{}&quo…

从0开始的操作系统手搓教程21:进程子系统的一个核心功能——简单的进程切换

目录 具体说说我们的简单RR调度 处理时钟中断处理函数 调度器 schedule switch_to 我们下面&#xff0c;就要开始真正的进程切换了。在那之前&#xff0c;笔者想要说的是——我们实现的进程切换简单的无法再简单了——也就是实现一个超级简单的轮询调度器。 每一个进程按照…

windows下Jmeter的安装与使用

一、下载地址 官网地址&#xff1a;Apache JMeter - Download Apache JMeter Binaries&#xff1a;已经编绎好的二进制文件&#xff0c;可直接执行&#xff0c;下载解压后就可以使用。 Source&#xff1a;源代码文件&#xff0c;需要自己编绎才可以执行。 二、环境变量 我看…

npm : 无法加载文件 C:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本。

1、在 vscode 终端执行 get-ExecutionPolicy 返回 Restricted 状态是禁止的 返回 RemoteSigned 状态是可正常执行npm命令 2、更改状态 set-ExecutionPolicy RemoteSigned 如果提示需要管理员权限&#xff0c;可加参数运行 Set-ExecutionPolicy -Scope CurrentUser RemoteSi…

XMOS推出“免开发固件方案”将数字接口音频应用的开发门槛大幅降低

使用该套“免开发固件方案”可将开发周期从三个月缩短到14天 中国深圳&#xff0c;2025年3月——全球领先的软件定义系统级芯片&#xff08;SoC&#xff09;开发商XMOS宣布&#xff1a;公司已推出了“免开发固件方案”&#xff0c;可实现中高端音频解决方案的0代码开发。与传统…

SpringBoot项目集成ElasticSearch

1. 项目背景 处于失业找工作的阶段&#xff0c;随便写写吧~ 没啥背景&#xff0c;没啥意义&#xff0c;Java后端越来越卷了。第一学历不是本科&#xff0c;感觉真的是没有一点路可走。 如果有路过的小伙伴&#xff0c;如果身边还有坑位&#xff0c;不限第一学历的话&#xff0…

初识Python:一门简洁而强大的编程语言

今天带来一期关于python的相关知识&#xff0c;用于新手开发python使用阅读&#xff0c;后续会出相关文章&#xff0c;大家请关注&#xff0c;博主尽量日更&#xff01;&#xff01;&#xff01; 前言 在当今的编程世界中&#xff0c;Python无疑是最受欢迎的语言之一。无论是初…

Mayavi一个强大的python库

Mayavi 介绍 Mayavi 是一个用于 Python 的科学数据可视化库,提供了一种便捷的方式来创建复杂的 3D 可视化效果。它基于 VTK(Visualization Toolkit)构建,能够处理各种类型的数据,包括标量、矢量和张量数据,广泛应用于科学研究和数据分析领域。 主要特点 丰富的可视化选…