From 7d2ec9c1ab62d5ec111106eacd4e6443da9075a8 Mon Sep 17 00:00:00 2001 From: xiepengfei Date: Fri, 23 Oct 2026 14:41:43 +0800 Subject: [PATCH] fix: reduce print preview delay by yielding UI events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Root cause: updateHighlighterAll() rehighlights every block synchronously without yielding, blocking UI thread for ~6s on medium files during print preview 2. Fix: call qApp->processEvents() every 100 blocks to keep UI responsive, consistent with cloneLargeDocument() pattern 3. Cleanup: remove dead DPrinter printer(QPrinter::HighResolution) in popupPrintDialog(), introduced by commit 9fcf3e4c, which triggers unnecessary cups plugin load and printer enumeration 4. Impact: print preview opens faster, no behavior change for syntax highlighting or print output Log: 优化打印预览页面打开速度,减少 UI 阻塞 Influence: 1. Test print preview with medium/large syntax-highlighted files 2. Verify syntax highlighting renders correctly in print preview 3. Test print preview with plain text files (no highlighter) 4. Verify no -Wunused-variable warnings after dead code removal fix: 优化打印预览页面响应速度,减少 UI 线程阻塞 1. 根因:updateHighlighterAll() 同步遍历所有文本块执行语法高亮, 无 processEvents 间歇,中小文件打印预览时阻塞 UI 线程约 6 秒 2. 方案:循环中每 100 块调用 qApp->processEvents() 保持 UI 响应, 与 cloneLargeDocument() 现有模式一致 3. 清理:删除 popupPrintDialog() 中 DPrinter printer(QPrinter:: HighResolution) 死代码,由 commit 9fcf3e4c 引入,会触发不必要的 cups 插件加载和打印机枚举 4. 影响:打印预览打开速度提升,语法高亮和打印输出行为不变 Log: 优化打印预览页面打开速度,减少 UI 阻塞 Influence: 1. 测试中等/大型语法高亮文件的打印预览 2. 验证打印预览中语法高亮渲染正确 3. 测试纯文本文件(无语法高亮)的打印预览 4. 验证删除死代码后无 -Wunused-variable 告警残留 PMS: BUG-375053 --- src/editor/editwrapper.cpp | 4 ++++ src/widgets/window.cpp | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/editor/editwrapper.cpp b/src/editor/editwrapper.cpp index f8642463..c48eefd8 100644 --- a/src/editor/editwrapper.cpp +++ b/src/editor/editwrapper.cpp @@ -1451,8 +1451,12 @@ void EditWrapper::updateHighlighterAll() return; } + int blockCount = 0; for (QTextBlock var = beginBlock; var != endBlock; var = var.next()) { m_pSyntaxHighlighter->rehighlightBlock(var); + if (++blockCount % 100 == 0) { + qApp->processEvents(); + } } m_pSyntaxHighlighter->rehighlightBlock(endBlock); diff --git a/src/widgets/window.cpp b/src/widgets/window.cpp index 91a9af10..e6a0ad16 100644 --- a/src/widgets/window.cpp +++ b/src/widgets/window.cpp @@ -2441,7 +2441,6 @@ void Window::popupPrintDialog() return; } - DPrinter printer(QPrinter::HighResolution); m_isNewPrint = true; m_pPreview = new DPrintPreviewDialog(this); m_pPreview->setAttribute(Qt::WA_DeleteOnClose);