From ba01759c353555e52edf72643aaa27614abba4e5 Mon Sep 17 00:00:00 2001 From: lzwind Date: Sun, 15 Nov 2026 12:56:15 +0800 Subject: [PATCH] fix: show error when print save fails to no-permission dir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Root cause: DPrintPreviewDialog (DTK6) only exposes paintRequested signal, no error/failure/saved signal. DocSheet::onPopPrintDialog() connects only paintRequested, has no way to detect save failures. When user saves PDF to a no-permission directory, DTK internal save fails silently, dialog closes, no error shown to user. 2. Fix: save the DPrinter pointer in onPrintRequested callback, then connect QDialog::accepted signal in onPopPrintDialog to check if the output file was actually created via QFile::exists(). If the save path is non-empty (meaning save-to-file mode) but the file does not exist, show a DMessageBox warning prompting possible permission failure. 3. Impact: only affects print preview save-to-file flow; physical printer and normal save paths are unaffected; no API or behavior change for existing callers. Log: Fix print preview not showing error when saving to no-permission directory Influence: 1. Test print preview save to a writable directory (normal path) 2. Test print preview save to a read-only directory (error prompt shown) 3. Test print preview cancel operation (no false error prompt) 4. Test physical printer printing (no file existence check triggered) fix: 修复打印预览保存到无权限目录时无失败提示 1. 根因:DPrintPreviewDialog(DTK6)仅暴露 paintRequested 信号,无错误/ 失败/保存成功信号。DocSheet::onPopPrintDialog() 仅连接 paintRequested, 无法检测保存失败。用户保存 PDF 到无权限目录时,DTK 内部保存静默失败, 对话框关闭,用户无任何错误提示。 2. 方案:在 onPrintRequested 回调中保存 DPrinter 指针,在 onPopPrintDialog 中连接 QDialog::accepted 信号,检查输出文件是否实际创建(QFile::exists)。 若保存路径非空(即保存到文件模式)但文件不存在,弹出 DMessageBox 提示 可能是权限不足导致保存失败。 3. 影响:仅影响打印预览保存到文件的流程;物理打印和正常保存路径不受影响; 对现有调用者无 API 或行为变化。 Log: 修复打印预览保存到无权限目录时无失败提示的问题 Influence: 1. 测试打印预览保存到有写权限的目录(正常路径) 2. 测试打印预览保存到只读目录(弹出错误提示) 3. 测试打印预览取消操作(无误报提示) 4. 测试物理打印机打印(不触发文件存在性检查) PMS: BUG-380955 --- reader/uiframe/DocSheet.cpp | 14 ++++++++++++++ reader/uiframe/DocSheet.h | 1 + 2 files changed, 15 insertions(+) diff --git a/reader/uiframe/DocSheet.cpp b/reader/uiframe/DocSheet.cpp index df567eb35..79db05acd 100644 --- a/reader/uiframe/DocSheet.cpp +++ b/reader/uiframe/DocSheet.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -1031,6 +1032,7 @@ void DocSheet::showTips(const QString &tips, int iconIndex) void DocSheet::onPrintRequested(DPrinter *printer, const QVector &pageRange) { + m_printPrinter = printer; qCDebug(appLog) << "Print requested for pages:" << pageRange; if (pageRange.isEmpty()) { qCWarning(appLog) << "Empty page range for printing"; @@ -1134,6 +1136,7 @@ void DocSheet::onPrintRequested(DPrinter *printer, const QVector &pageRange void DocSheet::onPrintRequested(DPrinter *printer) { + m_printPrinter = printer; // qCDebug(appLog) << "onPrintRequested"; printer->setDocName(QFileInfo(filePath()).fileName()); @@ -2082,6 +2085,17 @@ void DocSheet::onPopPrintDialog() //图片打印 connect(preview, QOverload::of(&DPrintPreviewDialog::paintRequested), this, QOverload::of(&DocSheet::onPrintRequested)); #endif + m_printPrinter = nullptr; + + connect(preview, &QDialog::accepted, this, [this]() { + if (m_printPrinter && !m_printPrinter->outputFileName().isEmpty()) { + QString savePath = m_printPrinter->outputFileName(); + if (!QFile::exists(savePath)) { + DMessageBox::warning(this, tr("打印"), tr("保存失败,目标目录可能没有写入权限。")); + } + } + }); + preview->open(); qCDebug(appLog) << "onPopPrintDialog end"; } diff --git a/reader/uiframe/DocSheet.h b/reader/uiframe/DocSheet.h index 4be2d37cd..e3c434527 100644 --- a/reader/uiframe/DocSheet.h +++ b/reader/uiframe/DocSheet.h @@ -1020,6 +1020,7 @@ private slots: bool m_needsRestoreTip = false; // 缓存的内容哈希,避免自动保存时反复读盘计算 QString m_cachedContentHash; + Dtk::Widget::DPrinter *m_printPrinter = nullptr; //打印预览对话框的打印机指针,用于关闭后检测保存是否成功 public: QProcess *m_process = nullptr; //当前调用的命令的进程地址